3 Operators
3 Operators
Studies
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 are used to perform operations on variables and values.
operands
sum =
a
+
operators b
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:
(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 ( ?? )
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 ?? Null-Coalescing
! Logical negation
>> Right shift ?: Conditional
~ Bitwise negation
> Less than = Assignment
++x Pre-increment operators
< Greater than +=
--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
14