Objects in this mirror are closer to Microsoft Technologies. DNM objective is to help users on Microsoft technologies by providing Articles, snippets, Interview Questions.

01 June 2012

Operators in C#

In this article we will discuss indetail about operators in C#.NET.

"Operators are used to perform mathematical and logical manipulations on data and variables".

Type of operators:
1.       Arithmetic operators
2.       Assignment operators
3.       Bitwise operators
4.       Conditional operators
5.       Increment and decrement operators
6.       Logical operators
7.       Relational operators

Arithmetic operators:
Can be used with any of numerical data type. You can’t use these on Boolean type.
Operator
Means
Usage (int x = 16;int y = 6;)
+
Addition or unary plus
x+y
22
-
Subtraction or unary minus
x-y
10
*
Multiplication
x*y
96
/
Division
x/y
2(decimal part truncated if y is integral value)
Note: if y is real value(y=6.0) the output will be 2.6666
%
Module division
Output sign is always sign of first operand
x%y
4Reminder of division
-x%y
-4 (minus due to x is minus value)
-x%-y
-4
x%-y
4 (plus due to x is plus value)
Example:
class OperatorUsage
    {
        public static void Main()
        {
            int x = 16;
            int y = 6;
            Console.WriteLine("{0}  + {1}  is: {2} ", x, y, x + y);
            Console.WriteLine("{0}  - {1}  is: {2}", x, y, x - y);
            Console.WriteLine("{0}  * {1}  is: {2}", x, y, x * y);
            Console.WriteLine("{0}  / {1}  is: {2}", x, y, x / y);
            Console.WriteLine("{0}  % {1}  is: {2}", x, y, x % y);
            Console.WriteLine("{0} % {1}  is: {2}", -x, y, -x % y);
            Console.WriteLine("{0} % {1} is: {2}", -x, -y, -x % -y);
            Console.WriteLine("{0}  % {1} is: {2}", x, -y, x % -y);
            Console.Read();
        }
    }
Output:


Note: in division (/) if any of operand is real value then result will be real.
Like 16/6.0 result is 2.666666
16/6 result is 2   

Assignment operators:
Used to assign the value of expression to a variable.
Operator
Means
Usage(int X = 10)
Result
=
Assignment
X=12
+=
Incremental addition
X +=3 à X=X+3
13
-=
Incremental subtratction
X -=3 à X=X-3
7
*=
Multiply by
X *=3 à X=X*3
30
/=
Divide by
X /=3à X=X/3
3
%=
Modulus or reminder by
X %=3 à X=X%3
1
<<=
Left shift
X <<=Y àX=X << Y
>>=
Right shift
X>>=Y è X=X>>Y
&=
Logical AND
X &=Y à X=X&Y
|=
Logical OR
X|=Y à X=X|Y






Bitwise operators:
Operator
Means
&
Bitwise logical AND
|
Bitwise logical OR
^
Bitwise logical XOR
~
One’s compliment
<< 
Shift left
>> 
Right shift


Conditional Operator:
Syntax:
Expression? true_condioned_expression: false_conditioned_expression
   class ConditionalOperatorUsage
    {
        public static void Main()
        {
            int x = 10;
            int y = 5;
            int result;
            result = (x > y) ? x : y;
            Console.WriteLine("{0} > {1} ? {0} : {1} and result is {2}", x, y, result);
            Console.Read();
        }
    }
Result:

The conditional operation same as like if else. The above example can be written like
Using conditional operator
à
Using if else
result = (x > y) ? x : y;
if (x > y)
result = x;
else
result = y;

Increment and decrement operators:
Operator
Usage(int X = 10)
Result
++
X++
10
++X
11
--
X--
10
--X
9

Example:
class IncrementDecrementOperator
    {
        public static void Main()
        {
            int x = 10;
            int y;
            Console.WriteLine("x={0}",x);
            y = x++;
            Console.WriteLine("y = x++; x={0},y={1}", x, y);
            y = ++x;
            Console.WriteLine("y = ++x; x={0},y={1}", x, y);
            x = 10;
            y = x--;
            Console.WriteLine("y = x--; x={0},y={1}", x, y);
            y = --x;
            Console.WriteLine("y = --x; x={0},y={1}", x, y);
            Console.Read();
        }
    }
Output:


Logical Operators:
Operator
Means
Usage(int X = 10)
Result
&&
Logical AND
||
Logical OR
!
Logical NOT
&
Boolean logical AND
|
Boolean logical OR
^
Boolean logical exclusive OR

Example:
class LogicalOperatorsUsage
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Truth tables for logical Operators\n");
            // Logical AND(&&)
            Console.WriteLine("{0}\n{1}: {2}\n{3}: {4}\n{5}: {6}\n{7}: {8}\n",
               "Logical AND (&&)", "false && false", (false && false),
               "false && true", (false && true),
               "true && false", (true && false),
               "true && true", (true && true));
            // Logical OR(||)
            Console.WriteLine("{0}\n{1}: {2}\n{3}: {4}\n{5}: {6}\n{7}: {8}\n",
               "Logical OR (||)", "false || false", (false || false),
               "false || true", (false || true),
               "true || false", (true || false),
               "true || true", (true || true));
            // Boolean logical NOT(|)
            Console.WriteLine("{0}\n{1}: {2}\n{3}: {4}",
               "Logical NOT (!)", "!false", (!false),
               "!true", (!true));
           
            // Boolean logical AND(&)
            Console.WriteLine("{0}\n{1}: {2}\n{3}: {4}\n{5}: {6}\n{7}: {8}\n",
               "Boolean logical AND (&)", "false & false", (false & false),
               "false & true", (false & true),
               "true & false", (true & false),
               "true & true", (true & true));
            // Boolean logical OR(|)
            Console.WriteLine("{0}\n{1}: {2}\n{3}: {4}\n{5}: {6}\n{7}: {8}\n",
               "Boolean logical OR (|)",
               "false | false", (false | false),
               "false | true", (false | true),
               "true | false", (true | false),
               "true | true", (true | true));
            // Boolean logical exclusive OR(^)
            Console.WriteLine("{0}\n{1}: {2}\n{3}: {4}\n{5}: {6}\n{7}: {8}\n",
               "Boolean logical exclusive OR (^)",
               "false ^ false", (false ^ false),
               "false ^ true", (false ^ true),
               "true ^ false", (true ^ false),
               "true ^ true", (true ^ true));
            Console.Read();
        }       
    }
Output:


Relational operators:
In case of comparisons we will use relational operators.
Operator
Means
Usage(int X = 10)
Result
==
Equals
X == 10
True
!=
Not equals
X != 10
False
Less than
X < 2
True
Greater than
X > 50
False
<=
Less than or equal to
X <= 5
False
>=
Greater than or equal to
X >= 8
True

0 Comments:

Post a Comment