An operator is a symbol to perform specific mathematical or logical operations.
C# has the following type of operators −
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Miscellaneous Operators
- Addition: The ‘+’ operator adds two values. For example, x+y.
- Subtraction: The ‘-‘ operator subtracts two values. For example, x-y.
- Multiplication: The ‘*’ operator multiplies two values. For example, x*y.
- Division: The ‘/’ operator divides the first values by the second. For example, x/y.
- Modulus: The ‘%’ operator returns the remainder when the first value is divided by the second.
For example, x%y.
Relational operators are used for comparison of two values.
- ==: Checks if the left and right side values are equal or not, if equal then the condition becomes true.
For example, 2==2 is true.
- !=: Checks if the left and right side values are not equal or not. For example, 4!=2 is true
- >: Checks if the left-side value is greater than the right side value. For example, 4>3 is true.
- <: Checks if the left-side value is less than the right side value. For example, 2<3 is true
- >=: Checks if the left-side value is greater or equal to the right side value.
For example, 4>=3 is true.
- <=: Checks if the left-side value is less than or equal to the right side value.
For example, 3<=3 is true
We have Logical AND, Logical OR, and Logical NOT operators.
Logical AND: The ‘&&’ operator returns true when both the conditions are true. If not it
returns false.
For example: 3>2 && 4>2 returns true as 3>2 is true and 4?2 is true.
Logical OR: The ‘||’ operator returns true when one (or both) of the conditions is true. If
not it returns false.
For example: 2>1 || 4>8 returns true as 2>1 is true and 4>8 is false. Here one of them is
true so it returns true.
Logical NOT: The ‘!’ operator returns true when the condition is not satisfied. If not it
returns false.
For example: !(2>4) returns true as 2>4 is false.
There are 6 bitwise operators which are
- Bitwise AND (&)
- Bitwise OR(|)
- Bitwise XOR(^)
- Left Shift(<<)
- Right Shift(>>)
& - It checks both bits and if both are 1 then the result of AND is 1.
| - It checks both bits and if any of those are 1 then the result of OR is 1.
^ - It does XOR of every bit of 2 numbers. If the 2 bits are different then the result is 1.
<< - It has two numbers, the first value is to left-shift the bits and the second value tells the
number of places to shift.
>> - It has two numbers, the first value is to right shift the bits and the second value tells
the number of places to shift.
~ - Binary One's Complement Operator is unary and flips the bits.
This Operator used to assign a value to a variable. The left side of the operator is a variable and
the right side is the value.
“=”(Simple Assignment): To assign a right side value to the left side variable.
E.g: x=100;
“+=”(Add AND assignment operator): It performs add and assign operations. It first
adds the left side variable value to the right-side value and assigns the result to the left
side variable. X+=Y is similar to X=X+Y.
“-=”(Subtract AND assignment operator): It performs subtract and assign operations. It
first subtracts the left side variable value to the right-side value and assigns the result to
the left side variable. X-=Y is similar to X=X-Y.
“*=”(Multiply AND assignment operator): It performs multiplication and assign
operations. It first multiplies the left side variable value to the right-side value and
assigns the result to the left side variable. X*=Y is similar to X=X*Y.
“/=”(Divide AND assignment operator): It performs division and assign operations. It
first divides the left side variable value to the right-side value and assigns the result to
the left side variable. X/=Y is similar to X=X/Y.
“%=”(Modulus AND assignment operator): It performs modulus and assigns operations.
It first performs modulo of the left side variable value to the right-side value and assigns
the result to the left side variable. X%=Y is similar to X=X%Y.
<<=(Left shift AND assignment operator): It performs left shift and assigns operations. It
first performs the left shift of the left side variable value by the value on the right-side
value and then assigns the result to the left side variable. X<<=2 is similar to X=X<<2.
>>==(Right shift AND assignment operator): It performs right shift and assigns
operations. It first performs the right shift of the left side variable value by the value on the
right-side value and then assigns the result to the left side variable. X>>=2 is similar to
X=X>>2.
&=(Bitwise AND assignment operator): It performs Bitwise AND and assign operations.
It first performs the Bitwise AND on the left side variable value by the value on the right-side value and then assigns the result to the left side variable. X&=2 is similar to X=X&2.
^=( Bitwise exclusive OR and assignment operator): It performs Bitwise exclusive OR
and assign operations. It first performs the Bitwise exclusive OR on the left side variable
value by the value on the right-side value and then assigns the result to the left side
variable. X^=2 is similar to X=X^2.
|=( Bitwise inclusive OR and assignment operator): It performs Bitwise inclusive OR
and assign operations. It first performs the Bitwise inclusive OR on the left side variable
value by the value on the right-side value and then assigns the result to the left side
variable. X|=2 is similar to X=X|2.
Conditional Expression (?:): This is also known as a Ternary operator.
Expression- If X is true? Y: otherwise Z
Here if the value of X is true then it results in the Y value otherwise Z value.
For Example X>20? 50: 100. If X=10, then the result is 100 as X>20 is false.
If X=21 or a value greater than 20 then the result is 50 as X>20 is true.