University of Computer Studies
Chapter 2
Operators
1
Contents
What is Operator?
Arithmetic Operators
Comparison Operators
Logical Operators
Bitwise Operators
Assignment Operators
Special Operators
Conditional Operator ( ?: )
Null – Coalescing Operator ( ?? )
Operator Precedence
2
Operator
Operators are used to perform operations on variables and values.
Operators in C# are some special symbols that perform some action on operands.
operands
sum = a + b
operators
There are two types of operators in C# : Unary operators and Binary operators.
Unary operators act on single operand.
Binary operators act on two operands (left-hand side and right-hand side operand of an operator). 3
Operator
C# includes the following categories of operators:
Arithmetic operators
Assignment operators
Comparison operators
Equality operators
Logical operators
Bitwise operators
Member access operators
Type-cast operators
4
Arithmetic Operators
Arithmetic Operators
Table 1
Operator Meaning Example Result
++ Increment x++ (or) ++x x=x+1
-- Decrement x-- (or) -- x x = x-1
+ Addition 5+3 8
- Subtraction 6–1 5
* Multiplication 4*3 12
/ Division 21 / 5 4.2
% Modulus 21 % 5 1
+ Unary plus +x x = +x
- Negation -x x = -x
5
Comparison Operators
Comparison Operators
Table 2
Operator Meaning Example Result
< Less than x< y true if x is less than y
<= Less than or equal to x<=y true if x is less than or equals to y
> Greater than x>y true if x is greater than y
>= Greater than or equal to x >= y true if x is greater than or equals to y
== Equals x == y true if x equals to y
!= Not Equals x != y true if x does not equal to y
6
Logical Operators
Logical Operators
Table 3
Operator Meaning Example Result
! Not (Negation) !A true if A is false
& AND A& B true if both A and B are true
| OR A| B true if A or B ( or ) both are true
^ Xor (Exclusive Or) A^B true if A is true or B is true but both are
no true
&& AND A && B true if both A and B are true
|| OR A || B true if A or B ( or ) both are true
7
Bitwise Operators
Bitwise Operators
Table 4
Operator Meaning
& bitwise logical AND
| bitwise logical OR
^ bitwise logical Xor
~ one’s complement
<< shift left
>> shift right
8
Assignment Operators
Assignment Operators
Table 5
Operator Example
= a=b
+= a += b
-= a-= b
*= a*= b
/= a/=b
%= a%=b
<<= a<<= 3
>>= a>>= 3
&= a &=b
|= a |= b
^= a ^=b
9
Logical Operators
Special Operators
Table 6
Operator Meaning
is relational operator
as Relational operator
typeof type operator
sizeof size operator
new object creator
. (dot) member access operator
checked overflow checking
unchecked Prevention of overflow checking
10
Conditional Operator ( ?: )
a decision-making operator “ ?: ”
Also called conditional operator or ternary operator
short form of the if else conditions
Syntax:
(condition)? statement 1 : statement 2
Example:
int x = 20, y = 10;
string result = ( x > y )? “x is greater than y” : “x is less than y” ;
11
Null – Coalescing Operator ( ?? )
used with the nullable value types and reference types
If the value of the first operand is null, then the operator returns the value of the second operand, otherwise, it returns the value
of the first operand.
Example:
int? num1 = null;
num2 = num1 ?? 30;
int num3 = 50;
int num5 = 100
num4 = num3 ?? num5;
12
Operator Precedence
Table 7
Operator Description * Multiplication == Equals
() Grouping / Division != Not Equal
(parentheses)
% Modulus & Logical AND
x++ Post-increment
+ Concatenation ^ Logical Xor
x-- Post-decrement | Logical OR
+ Addition
+ Unary Plus && Conditional AND
- Subtraction
- Numeric negation || Conditional OR
<< Left shift
! Logical negation ?? Null-Coalescing
>> Right shift
?: Conditional
~ Bitwise negation > Less than
= Assignment
++x Pre-increment < Greater than operators
+=
--x Pre-decrement >= Less than or equal to
-=
(T) Casting <= Greater than or equal
…..
is to
Example: Inherits from
result = (a + b) * c / d; 13
Thank You
14