C# - Operators
Operators are used to perform operation on two operands. Operators in C# can be categorized as follows:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- Bitwise operators
Arithmetic operators are used to perform arithmetic operations on two operands.
|%||Modulus||a%b returns remainder of a/b (ex: 10 % 3 returns 1)|
Assignment operators are used to assign values of right hand side expression to left hand side operand.
|=||a = 5||a = 5|
|+=||a += b||a = a + b|
|-=||a -= b||a = a - b|
|*=||a *= b||a = a * b|
|/=||a /= b||a = a / b|
|%=||a %= b||a = a % b|
|&=||a &= b||a = a & b|
||=||a |= b||a = a | b|
|^=||a ^= b||a = a ^ b|
|>>=||a >>= b||a = a >> b|
|<<=||a <<= b||a = a << b|
Comparison operators are used to compare values of two operands. It returns true when values matches and false when values doen not match.
|>=||Greater than or equal to|
|<=||Less than or equal to|
Increment and decrement operators are used to increase and decrease the value of variable.
|++x||Pre-increment: Increases the value of x by 1, then returns x.|
|x++||Post-increment: Returns x, then increases the value of x by 1.|
|--x||Pre-decrement: Decreases the value of x by 1, then returns x.|
|x--||Post-decrement: Returns x, then decreases the value of x by 1.|
Logical operators are used to combine two or more conditions.
|&&||AND||Returns True when all conditions are true|
|||||OR||Returns True when any of the conditions is true|
|!||NOT||Returns opposite result: !(2<5) returns false|
Bitwise operators are used to perform bitwise operations on two operands.
|&||AND||Returns 1 if both bits at the same in both operands are 1, else returns 0|
||||OR||Returns 1 if one of two bits at the same in both operands is 1, else returns 0|
|^||XOR||Returns 1 if only one of two bits at the same in both operands is 1, else returns 0|
|~||NOT||Reverse all the bits|
|>>||Right shift||The left operand is moved right by the number of bits present in the right operand|
|<<||Left shift||The left operand value is moved left by the number of bits present in the right operand|