Ads block

Banner 728x90px


C# Operators

Operator in C# is a special symbol that specifies which operations to perform on operands. For example, in mathematics the plus symbol (+) signifies the sum of the left and right numbers. In the same way, C# has many operators that have different meanings based on the data types of the operands. C# operators usually have one or two operands. Operators that have one operand are called Unary operators.
The following table list some of the operators available in C#.
Operator categoryOperators
Multiplicativex * y
Additivex + y
Shiftx << y
Relational and type testingx < y
Equalityx == y
Logical ANDx & y
Logical XORx ^ y
Logical ORx | y
Conditional ANDx && y
Conditional ORx || y
Null-coalescingx ?? y
Assignment and lambda expressionx = y
As mentioned before, certain operators have different meanings based on the datatype of the operand. For example, if the + operator is used with numbers, it will add the numbers but if it is used with strings, it will concatenate the two strings.
When an operator does different things based on the datatype of the operands, it is called operator over loading.
The following C# code shows the use of the + sign operator on different datatypes:
Example: + Operator
static void Main(string[] args)
    string message1 = "Hello";

    string message2 = message1 + " World!!";    


    int i = 10, j = 20;

    int sum = i + j;

    Console.WriteLine("{0} + {1} = {2}", i, j, sum);


Hello World!! 
10 + 20 = 30.

This tutorial doesn't cover detail of each operators. Visit MSDN to learn all the operators in detail.

No comments:

Post a Comment