Professional Documents
Culture Documents
3 Operators
3 Operators
3 Operators
Chapter 2
Operators
1
Contents
What is Operator?
Arithmetic Operators
Comparison Operators
Logical Operators
Bitwise Operators
Assignment Operators
Special Operators
Conditional Operator ( ?: )
Operator Precedence
2
Operator
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.
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
Type-cast operators
4
Arithmetic Operators
Arithmetic Operators
Table 1
6
Logical Operators
Logical Operators
Table 3
^ Xor (Exclusive Or) A^B true if A is true or B is true but both are
no 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
10
Conditional Operator ( ?: )
a decision-making operator “ ?: ”
Also called conditional operator or ternary operator
short form of the if else conditions
Syntax:
Example:
int x = 20, y = 10;
string result = ( x > y )? “x is greater than y” : “x is less than y” ;
11
Null – Coalescing Operator ( ?? )
Example:
int? num1 = null;
num2 = num1 ?? 30;
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