An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.C# has rich set of built-in operators and provides the following type of operators
Common operators:
Arithmetic Operators like +,-,*,/,%
Assignment Operator =,+=,-=,*=,/=,%=
Comparison Operators like ==, !=,>, >=, <, <=
Conditional Operators like &&, ||
Ternary Operator ?:
Null Coalescing Operator ??
Example:
using System;
class Program
{
static void Main()
{
int a = 21;
int b = 10;
int c;
Console.WriteLine("Examples of Arithmetic operators");
c = a + b;
Console.WriteLine(" 1 - Value of c is {0}", c);
c = a - b;
Console.WriteLine(" 2 - Value of c is {0}", c);
c = a * b;
Console.WriteLine(" 3 - Value of c is {0}", c);
c = a / b;
Console.WriteLine(" 4 - Value of c is {0}", c);
c = a % b;
Console.WriteLine(" 5 - Value of c is {0}", c);
Console.WriteLine("Assignment operators");
c = a;
Console.WriteLine(" 1 - = Value of c = {0}", c);
c += a;
Console.WriteLine(" 2 - += Value of c = {0}", c);
c -= a;
Console.WriteLine(" 3 - -= Value of c = {0}", c);
c *= a;
Console.WriteLine(" 4 - *= Value of c = {0}", c);
c /= a;
Console.WriteLine(" 5 - /= Value of c = {0}", c);
c = 200;
c %= a;
Console.WriteLine(" 6 - %= Value of c = {0}", c);
Console.WriteLine("Comparison operators");
if (a == b)
{
Console.WriteLine(" 1 - a is equal to b");
}
else
{
Console.WriteLine(" 1 - a is not equal to b");
}
if (a < b)
{
Console.WriteLine(" 2 - a is less than b");
}
else
{
Console.WriteLine(" 2 - a is not less than b");
}
if (a > b)
{
Console.WriteLine(" 3 - a is greater than b");
}
else
{
Console.WriteLine(" 3 - a is not greater than b");
}
if (a <= b)
{
Console.WriteLine(" 4 - a is either less than or equal to b");
}
if (b >= a)
{
Console.WriteLine(" 5 - b is either greater than or equal to b");
}
if (a !=b )
{
Console.WriteLine(" 6 - a is not equal to b");
}
Console.WriteLine("conditional operators");
bool d = true;
bool e = true;
if (d && e)
{
Console.WriteLine(" 1 - Condition is true");
}
if (d || e)
{
Console.WriteLine(" 2 - Condition is true");
}
/* lets change the value of a and b */
d = false;
e = true;
if (d && e)
{
Console.WriteLine(" 3 - Condition is true");
}
else
{
Console.WriteLine(" 3 - Condition is not true");
}
if (!(d && e))
{
Console.WriteLine(" 4 - Condition is true");
}
}
}
Ternary Operator ?:
The ternary operator tests a condition. It compares two values. It produces a third value that depends on the result of the comparison. This can be accomplished with if-statements or other constructs.This can be accomplished with if-statements or other constructs.
Program without ternary operator:
using System;
class Program
{
static void Main()
{
int a = 10;
bool IsA10;
if (a == 10)
{
IsA10 = true;
}
else
{
IsA10 = false;
}
Console.WriteLine("i == 10 is {0}", IsA10);
Console.ReadLine();
}
}
Program with ternary operator:using System;
class Program
{
static void Main()
{
int a = 10;
bool IsA10 = a == 10 ? true : false;
Console.WriteLine("i == 10 is {0}", IsA10);
}
}
One common use of the ternary operator is to initialize a variable with the result of the expression. It reduces multiple if-statements and nesting.
Null Coalescing Operator ??
Before discussing about null coalescing operator first we should know about nullable types in c#
Nullable types in c#
In C# types are divided into 2 broad categories.
Value Types - int, float, double, structs, enums etc..
Reference Types – Interface, Class, delegates, arrays etc..
By default value types are non nullable. To make them nullable use ?
int i = 0 (i is non nullable, so "i" cannot be set to null, i = null will generate compiler error)
int? j = 0 (j is nullable int, so j=null is legal)
Nullable types bridge the differences between C# types and Database types
The null coalescing operator "??" uses two question marks. With it you can use a custom value for a null reference variable. It simplifies null tests.
Program without using NULL coalescing operator
using System;
class Program
{
static void Main()
{
int AvailableTickets;
int? TicketsOnSale = null;
if (TicketsOnSale == null)
{
AvailableTickets = 0;
}
else
{
AvailableTickets = (int)TicketsOnSale;
}
Console.WriteLine("Available Tickets={0}", AvailableTickets);
}
}
Program with using NULL coalescing operator
using System;
using System;
class Program
{
static void Main()
{
int AvailableTickets;
int? TicketsOnSale = null;
//Using null coalesce operator ??
AvailableTickets = TicketsOnSale ?? 0;
Console.WriteLine("Available Tickets={0}", AvailableTickets);
Console.ReadLine();
}
}
0 comments:
Post a Comment