Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

C#

Learn step by step



Operators

An operator is a special symbol which indicates a certain process is carried out. Operators in programming languages are taken from mathematics. Programmers work with data. The operators are used to process data. An operand is one of the inputs (arguments) of an operator.
The following table shows a set of operators used in the C# language.

Category Symbol
Sign operators + -
Arithmetic + - * / %
Logical (boolean and bitwise) & | ^ ! ~ && || true false
String concatenation +
Increment, decrement ++ --
Shift << >>
Relational == != < > <= >=
Assignment = += -= *= /= %= &= |= ^= <<= >>=
Member access .
Indexing []
Cast ()
Ternary ?:
Delegate concatenation and removal + -
Object creation new
Type information as is sizeof typeof
Overflow exception control checked unchecked
Indirection and address * -> [] &
Lambda =>

An operator usually has one or two operands. Those operators that work with only one operand are called unary operators. Those who work with two operands are called binary operators. There is also one ternary operator (?:), which works with three operands. Certain operators may be used in different contexts. For example the + operator. From the above table we can see, that it is used in different cases. It adds numbers, concatenates strings or delegates; indicates the sign of a number. We say, that the operator is overloaded.

Arithmetic Operators

Following table shows all the arithmetic operators supported by C#. Assume variable A holds 10 and variable B holds 20 then

OperatorDescriptionExample
+Adds two operands A + B will give 30
-Subtracts second operand from the first A - B will give -10
*Multiplies both operands A * B will give 200
/Divides numerator by de-numerator B / A will give 2
%Modulus Operator and remainder of after an integer division B % A will give 0
++Increment operator increases integer value by one A++ will give 11
--Decrement operator decreases integer value by one A-- will give 9


using System;

namespace opp
{
class MyProgram
{
static void Main(string[] args)
{
int a = 210;
int b = 10;
int c;

c = a + b;
Console.WriteLine(" Value of c is {0}", c);
c = a - b;
Console.WriteLine(" Value of c is {0}", c);
c = a * b;
Console.WriteLine(" Value of c is {0}", c);
c = a / b;
Console.WriteLine(" Value of c is {0}", c);
c = a % b;
Console.WriteLine(" Value of c is {0}", c);
c = a++;
Console.WriteLine(" Value of c is {0}", c);
c = a--;
Console.WriteLine(" Value of c is {0}", c);
Console.ReadLine();
}
}
}

Output

Value of c is 220
Value of c is 200
Value of c is 2100
Value of c is 21
Value of c is 0
Value of c is 211
Value of c is 210
Relational Operators

Following table shows all the relational operators supported by C#. A holds 10 and variable B holds 20.

OperatorDescriptionExample
== Checks if the values of two operands are equal or not, if yes then condition becomes true. (A == B) is not true.
!= Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true.
> Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true.
< Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true.
>= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true.
<= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.


using System;

class MyProgram
{
static void Main(string[] args)
{
int a = 210;
int b = 10;

if (a == b)
{
Console.WriteLine("a is equal to b");
}
else
{
Console.WriteLine("a is not equal to b");
}
if (a < b)
{
Console.WriteLine("a is less than b");
}
else
{
Console.WriteLine(" a is not less than b");
}
if (a > b)
{
Console.WriteLine("a is greater than b");
}
else
{
Console.WriteLine("a is not greater than b");
}
/* Lets change value of a and b */
a = 1;
b = 4;
if (a <= b)
{
Console.WriteLine("a is either less than or equal to b");
}
if (b >= a)
{
Console.WriteLine("b is either greater than or equal to b");
}
}
}

Output

a is not equal to b
a is not less than b
a is greater than b
a is either less than or equal to b
b is either greater than or equal to b
Logical Operators

OperatorDescriptionExample
&& Called Logical AND operator. If both the operands are non zero then condition becomes true. (A && B) is false.
||Called Logical OR Operator. If any of the two operands is non zero then condition becomes true. (A || B) is true.
!Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(A && B) is true.


using System;

namespace Opp
{
class Program
{
static void Main(string[] args)
{
bool a = true;
bool b = true;

if (a && b)
{
Console.WriteLine("Condition is true");
}
if (a || b)
{
Console.WriteLine("Condition is true");
}
/* lets change the value of a and b */
a = false;
b = true;
if (a && b)
{
Console.WriteLine(" Condition is true");
}
else
{
Console.WriteLine("Condition is not true");
}
if (!(a && b))
{
Console.WriteLine("Condition is true");
}
Console.ReadLine();
}
}
}

Output

Condition is true
Condition is true
Condition is not true
Condition is true
Bitwise Operators

Bitwise operator works on bits and perform bit by bit operation. following table show how bitwise operators are work

pqp & qp | qp ^ q
00000
01011
11110
10011

Assume variable A holds 60 and variable B holds 13 then:
OperatorDescriptionExample
& Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12. which is 0000 1100
| Binary OR Operator copies a bit if it exists in either operand. (A | B) will give 61, which is 0011 1101
^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) will give 49, which is 0011 0001
~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. (~A ) will give -61, which is 1100 0011 in 2's complement due to a signed binary number.
<< Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 will give 240, which is 1111 0000
>> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 will give 15, which is 0000 1111

Assignment Operators

OperatorDescriptionExample
=Simple assignment operator, Assigns values from right side operands to left side operand C = A + B will assign value of A + B into C
+=Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand C += A is equivalent to C = C + A
-=Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand C -= A is equivalent to C = C - A
*=Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand C *= A is equivalent to C = C * A
/=Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand C /= A is equivalent to C = C / A
%=Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand C %= A is equivalent to C = C % A
<<=Left shift AND assignment operator C <<= 2 is same as C = C << 2
>>=Right shift AND assignment operator C >>= 2 is same as C = C >> 2
&=Bitwise AND assignment operator C &= 2 is same as C = C & 2
^=bitwise exclusive OR and assignment operator C ^= 2 is same as C = C ^ 2
|=bitwise inclusive OR and assignment operator C |= 2 is same as C = C | 2


uusing System;

namespace OperatorsAppl
{
class Program
{
static void Main(string[] args)
{
int a = 21;
int c;

c = a;
Console.WriteLine("Value of c = {0}", c);

c += a;
Console.WriteLine("Value of c = {0}", c);

c -= a;
Console.WriteLine(" Value of c = {0}", c);

c *= a;
Console.WriteLine(" *= Value of c = {0}", c);

c /= a;
Console.WriteLine("/= Value of c = {0}", c);

c = 200;
c %= a;
Console.WriteLine(" %= Value of c = {0}", c);

c <<= 2;
Console.WriteLine("<<= Value of c = {0}", c);

c >>= 2;
Console.WriteLine(">>= Value of c = {0}", c);

c &= 2;
Console.WriteLine("&= Value of c = {0}", c);

c ^= 2;
Console.WriteLine("^= Value of c = {0}", c);

c |= 2;
Console.WriteLine("|= Value of c = {0}", c);
Console.ReadLine();
}
}
}

Output

= Value of c = 21
+= Value of c = 42
-= Value of c = 21
*= Value of c = 441
/= Value of c = 21
%= Value of c = 11
<<= Value of c = 44
>>= Value of c = 11
&= Value of c = 2
^= Value of c = 0
|= Value of c = 2
Misc Operators

OperatorDescriptionExample
sizeof()Returns the size of a data type.sizeof(int), will return 4.
typeof()Returns the type of a class.typeof(StreamReader);
&Returns the address of an variable.&a; will give actual address of the variable.
*Pointer to a variable.*a; will pointer to a variable.
? :Conditional Expression If Condition is true ? Then value X : Otherwise value Y
isDetermines whether an object is of a certain type.If( Ford is Car) // checks if Ford is an object of the Car class.
asCast without raising an exception if the cast fails.Object obj = new StringReader("Hello");
StringReader r = obj as StringReader;

Operator precedence

The operator precedence tells us which operators are evaluated first. The precedence level is necessary to avoid ambiguity in expressions.
What is the outcome of the following expression? 28 or 40?
3 + 5 * 5 Like in mathematics, the multiplication operator has a higher precedence than addition operator. So the outcome is 28.
(3 + 5) * 5 To change the order of evaluation, we can use parentheses. Expressions inside parentheses are always evaluated first.
The following table shows common C# operators ordered by precedence (highest precedence first):

Operator(s) Category Associativity
Primary x.y f(x) a[x] x++ x-- new typeof default checked unchecked Left
Unary + - ! ~ ++x --x (T)x Left
Multiplicative * / % Left
Additive + - Left
Shift << >> Left
Equality == != Right
Logical AND & Left
Logical XOR ^ Left
Logical OR | Left
Conditional AND && Left
Conditional OR || Left
Null Coalescing ?? Left
Ternary ?: Right
Assignment = *= /= %= += -= <<= >>= &= ^= |= => Right



Related Videos