# C# Operators

C# operators are used to calculate the result of expressions. The following is the big table of the operators. You can click the link to go the detail page.

Operator | Usage | Description | Examples |
---|---|---|---|

Arithmetic Operators | Set A=15 and B=5 | ||

+ | A + B | A plus B. | int C = A + B; //C will be 15+5=20 |

- | A - B | Subtract B from A | int C = A - B; //C will be 15-5=10 |

* | A x B | A times B | int C = A * B; //C will be 15X5=75 |

/ | A / B | A divided by B | int C = A / B; //C will be 15/5=3 |

% | A % B | A mod B returns the reminder of A divided by B. | int C = A % B; //C will be 15 % 5=0 |

++ | A++ | return the value of the variable before it adds 1. | int C = A++; //C will be 15 and A will be 16 |

++ | ++A | return the value of the variable after it adds 1. | int C = ++A; //A will be 16 and C will be 16 too. |

-- | A-- | return the value of the variable before it minus 1. | int C = A--; //C will be 15 and A will be 14 |

-- | --A | return the value of the variable after it minus 1. | int C = --A; //A will be 14 and C will be 14 too. |

Relational Operators | Set A=15 and B=5 | ||

== | A == B | Return true if A equals B otherwise false | bool C = A == B; //C is false because 15 does not equal to 5. |

!= | A != B | Return false if A equals B otherwise true | bool C = A != B; //C is true because 15 does not equal to 5. |

> | A > B | Return true if A is greater than B otherwise false | bool C = A > B; //C is true because 15 is greater than 5. |

< | A < B | Return true if A is less than B otherwise false | bool C = A > B; //C is false because 15 is not less than 5. |

>= | A >= B | Return true if A is no less than B otherwise false | bool C = A >= B; //C is true because 15 is no less than 5. |

<= | A <= B | Return true if A is no greater than B otherwise false | bool C = A >= B; //C is false because 15 is greater than 5. |

Logical Operators | Set A=true and B=false | ||

! | !A | Not A. It returns true if A is false and returns false if A is true. | bool C = !A; //C is false because A is true. |

& | A & B | A and B. It returns true only if both A and B are true otherwise false | bool C = A & B; //C is false because B is false. |

| | A | B | A or B. It returns false only if both A and B are false otherwise true | bool C = A | B; //C is true because A is true. |

^ | A ^ B | A XOR B. It returns true if only one of A or B is true otherwise false | bool C = A ^ B; //C is true because only A is true. |

&& | A && B | Same as Logical AND but only evaluate B if A is true | bool C = A && B; //C is false because B is false. |

|| | A || B | Same as Logical OR but only evaluate B if A is false | bool C = A || B; //C is true because A is true. |

Bitwise Operators | Set A=1(00000001) and B=2(00000010) | ||

~ | ~A | Returns the negation for each bit. | sbyte C = ~A; //C is 11111110. |

& | A & B | Returns binary AND for each bit of the operands. | sbyte C = A & B; //C is 00000000. |

| | A | B | Returns binary OR for each bit of the operands. | sbyte C = A | B; //C is 00000011. |

^ | A ^ B | Returns binary XOR for each bit of the operands. | sbyte C = A ^ B; //C is 00000011. |

<< | A << n | Return n times left shift bit of A. | sbyte C = A << 1; //C 00000010. |

>> | A >> n | Return n times right shift bit of A. | sbyte C = B >> 1; //C 00000001. |

Assignment Operators | Set A=16 and B=2 | ||

= | A=B | Simple assignment operator. Assign B's value to A. | A = B; //A is 2. |

+= | A+=B | Addition assignment operator. Equivalent to A = A + B | A+=B; //A is 18. |

-= | A-=B | Subtraction assignment operator. Equivalent to A = A - B | A-=B; //A is 14. |

*= | A*=B | Multiplication assignment operator. Equivalent to A = A * B | A*=B; //A is 32. |

/= | A/=B | Division assignment operator. Equivalent to A = A / B | A/=B; //A is 8. |

%= | A%=B | Remainder assignment operator. Equivalent to A = A % B | A%=B; //A is 0. |

&= | A&=B | AND assignment operator. Equivalent to A = A & B | A&=B; //A is 0. |

|= | A|=B | OR assignment operator. Equivalent to A = A | B | A|=B; //A is 18. |

^= | A^=B | Exclusive-OR assignment operator. Equivalent to A = A ^ B | A%=B; //A is 18. |

<<= | A<<=B | Left shift assignment operator. Equivalent to A = A << B | A<<=B; //A is 64. |

>>= | A>>=B | Right shift assignment operator. Equivalent to A = A >> B | A>>=B; //A is 4. |

For the other operators, we just introduce conditional operator ?: and examples. We'll introduce the others in later sections.

Finally, we demonstrate the operator precedence table and the example.