C# FOR BEGINNERS CRASH COURSE (2014)
Chapter 5 Operators in C#
5.1 Introduction to operators
Operator is used to define the meaning of an expression. It is a set of one or more characters used for computations or comparisons. Operators can change one or more data values, called operands into a new value.
5.2 Arithmetic operators
Arithmetic operators are used for performing the arithmetic operations on variables. The table shows the arithmetic operators in C#.
Operator |
Description |
Example |
+ |
Add two operands |
c=a+b If a=10, b=20, c=10+20=30 |
- |
Subtracts second operand from the first |
c=a-b If a=10, b=5, c=10-5=5 |
* |
Multiplies both the operands |
c=a*b If a=10, b=5, c=10*5=50 |
/ |
Divides the numerator by denominator |
c=a/b If a=21, b=2, c=21/2=10 |
% |
Modulus operator and remainder after integer division |
c=a%b If a=21, b=2, c=21%2=1 |
++ |
Increment operator. User for increasing value by one |
If a=10, a++=11 |
-- |
Decrement. The value can be decreased by one |
If a=10 a--=9 |
5.3 Relational operators
The relational operators are used for relational operations and type comparisons.
Operator |
Description |
Example |
== |
It checks if values of the operands are equal or not. |
If x=10, y=20, (x==y) is not true |
!= |
If the values of the two operands are not equal then condition is true |
If x=11, y=12, (x!=y) is true |
> |
If the left operand is greater than right, condition is true |
If x=13, y=17, (x>y) is not true |
< |
If the right operand is greater than left, condition is true |
If x>10, y=15, (x<y) is true |
>= |
If the value of the left operand is greater or equal to right, condition is true |
If x=5, y=3, (x>=y) is true |
<= |
If the value of the left operand is less or equal to right, condition is true |
If x=10, y=4, (x<=y) is not true |
5.4 Logical operators
Logical operators are used for evaluating an expression and return a Boolean value.
Operator |
Description |
Example |
&& |
Logical AND operator. If both the expressions are true, result is true |
X has Boolean value as true and Y has Boolean value false, (A&&B) is false |
! |
Logical NOT operator. If the expression is false, returns true |
X has Boolean value true, !(X) = false |
|| |
Logical OR operator. If either of the expression is true, result is true |
X has Boolean value true, Y has Boolean value false, (X||Y) is true |
^ |
If either of the expression is true, returns true. It returns false if both the expressions are true or false |
X has Boolean value true, Y has value false, (X^Y) is true |
5.5 Bitwise operators
Bitwise operators are used on bits. They perform bit operation.
a |
b |
a&b |
a|b |
a^b |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
1 |
1 |
0 |
0 |
1 |
1 |
1 |
1 |
1 |
1 |
0 |
5.6 Assignment operators
Assignment operators are used for performing arithmetic operations on the operands. The resultant value is assigned to any one of them.
Operator |
Description |
Example |
= |
Assigns value from right side operands to the left side operand |
z=x+y assigns value pf x+y to z |
+= |
Add AND assignment operator. Adds the right operand to the left operand and the result is assigned to left operand |
x+=y is similar to x=x+y |
-= |
Subtract AND assignment operator. Subtracts the right operand from the left. |
x-=y is similar to x=x-y |
*= |
Multiply AND assignment operator. Multiplies right operand with the left |
x*=y is similar to x=x*y |
/= |
Divide AND assignment operator. Divides left operand with the right |
x/=y is similar to x=x/y |
%= |
Modulus AND assignment operator. It takes the modulus of both the operands |
x%=y is similar to x=x%y |
<<= |
Left shift AND assignment operator |
a<<=2 is similar to a=a<<2 |
>>= |
Right shift AND assignment operator |
a>>=2 is similar to a=a>>2 |
&= |
Bitwise AND assignment operator |
a&=3 is similar to a=a&3 |
^= |
Bitwise exclusive OR assignment operator |
a^=4 is similar to a=a^4 |
|= |
Bitwise inclusive OR assignment operator |
a|=5 is similar to a=a | 5 |
5.7 Miscellaneous operators
Operator |
Description |
Example |
sizeof() |
It returns the data size |
sizeof(int), returns 4 |
typeof() |
The type of class is provided |
typeof(StreamWriter); |
& |
Address of an variable |
&x, actual address of the variable |
?: |
Conditional expression |
If condition is true? X value else Y |
as |
If the cast is not success, cast without raising exception |
Object o1 = new StringReader(“Welcome”); StringReader s = obj as StringReader; |
is |
It checks if the object is of specific type |
If(Maths is Subject) , Checks for the subject maths |