# C# Operators

C# comes up with different kind of operators, these operators are used to perform different kinds of manipulations. Each operator represents a specific operation. Following is the list of different types of operators.

- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Bitwise Operators
- Misc Operators
- Unary Operators
- Ternary Operators

Let's understand each type of operators one by one.

## Arithmetic Operators

In C# arithmetic operators are used to basic mathematical calculations. Following is the list of Arithmetic operators.

Operator | Description | Example (x=100, y=50) |
---|---|---|

`+` |
Used to add two values or operands | x+y=150 |

`-` |
Used to subtract second value from the expression | x-y=-50 |

`*` |
Used to Multiply two operands or values | x*y=5000 |

`/` |
Used to divide numerator by de-numerator | x/y=2 |

`%` |
Used to get the remainder after an integer division | x%y=0 |

## Relational Operators

Relational operators are used to compare two values of the same type. After comparison, it returns True or False based on the Condition. The Relational operators are known as comparison operators and Expressions that contain relational operators are called relational expressions. Relational operators are often used to check the conditions in a software application.

Operator | Description | Example (x=100, y=50) |
---|---|---|

`==` |
Used to check if two operands are equal or not. It returns true in case if both operands are equal else it returns false. | x==y returns false |

`!=` |
It works opposite of == operator. Returns true if two values are not same. It returns false if two values are same. | x!=y returns true |

`>` |
Used to check if the left value is greater than the value of right side. It returns true if the left side value is greater. | x>y returns true |

`<` |
Used to check if the left value is smaller than the value of right side. It returns true if the left side value is smaller. | x>y returns false |

`>=` |
Used to check if the left value is either greater or equal to the value of right side. It returns true if any conditions match. | x>=y returns true |

`<=` |
Used to check if the left value is smaller or equal to the value of right side. It returns true if any condition matches. | x<=y returns false |

## Logical Operators

Logical operators are used to compare two statements. After comparison, it returns True or False.

Operator | Description | Example (a=true, b=false) |
---|---|---|

`&&` |
The AND operator returns true if both operands have TRUE value. | a&&b returns false |

`||` |
The OR operator returns true if any operator from both has a TRUE value. | a||b returns true |

`!` |
NOT operator is used to reverse the operand's value. It converts TRUE into FALSE and vice versa. | !(a&&b) returns true |

## Assignment Operators

Assignment operator assigns the value of its right operand to its left operand. For example, if `x = y`

then this statement assigns the value of `y`

into `x`

. So if `y=10`

then after the execution of the statement the value of `x`

becomes 10. There are some other operators which are used with assignment (=) operator. Following is the list of assignment operators.

Operator | Description | Example If (a=100, b=50) |
---|---|---|

`=` |
= Operator works from Right to left. It assigns values from right side operand to left side operand. |
a = b Results a =50 |

`+=` |
+= Operator is called add AND operator. First, it sums both operands(left and right) value and then it assigns the resultant value to the left operand. |
a += b works same as a = a + b which Results a=150 |

`-=` |
-= named as Subtract AND assignment operator. First, it subtracts the right operand's value from the left operand value and then it assigns the resultant value to the left operand. | a -= b works same as a = a - b which Results a = 50 |

`*=` |
*= named as Multiply AND assignment operator. First, it multiplies both operands(left and right) value and then it assigns the resultant value to the left operand. | a *= b works same as a = a * b which Results a = 5000 |

`/=` |
/= named as divide AND assignment operator. First, it divides left operand value from the right operand value and then it assigns the resultant value to the left operand. | a /= b works same as a = a / b which Results a = 2 |

`%=` |
%= named as modules AND assignment operator. First, it divides left operand value from the right operand value and then it assigns modulus value to the left operand. | a %= b works same as a = a % b which Results a=0 |

`<<=` |
<<= named as left shift AND assignment operator. First, it left shifts, left side operand value to that number of times specified on the right side. and then it assigns the resultant value to the left operand. | a <<= 2 works same as a = a << 2 which Results a = 400 |

`>>=` |
>>= named as right shift AND assignment operator. First it right shifts, left side operand value to that number of times specified on the right side, and then it assigns the resultant value to the left operand. | a >>= 2 works same as a = a >> 2 which Results a = 25 |

`&=` |
&= named as AND assignment operator. It performs a bitwise logical AND operation then it assigns the resultant value to the left operand. | a &= b works same as a = a & b which Results a = 32 |

`^=` |
^= named as exclusive OR assignment operator. It performs a bitwise exclusive-OR operation and then it assigns the resultant value to the left operand. | a ^= b works same as a = a ^ b which Results a = 86 |

`|=` |
|= named as OR assignment operator. It performs a bitwise OR operation and then it assigns the resultant value to the left operand. | a |= b works same as a = a | b which Results a = 118 |