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.

OperatorUsageDescriptionExamples
Arithmetic OperatorsSet A=15 and B=5
+A + BA plus B.int C = A + B;  //C will be 15+5=20
-A - BSubtract B from Aint C = A - B;  //C will be 15-5=10
*A x BA times Bint C = A * B;  //C will be 15X5=75
/A / BA divided by Bint C = A / B;  //C will be 15/5=3
%A % BA 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
++++Areturn 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
----Areturn the value of the variable after it minus 1.int C = --A;  //A will be 14 and C will be 14 too.
Relational OperatorsSet A=15 and B=5
==A == BReturn true if A equals B otherwise falsebool C = A == B;  //C is false because 15 does not equal to 5.
!=A != BReturn false if A equals B otherwise truebool C = A != B;  //C is true because 15 does not equal to 5.
>A > BReturn true if A is greater than B otherwise falsebool C = A > B;  //C is true because 15 is greater than 5.
<A < BReturn true if A is less than B otherwise falsebool C = A > B;  //C is false because 15 is not less than 5.
>=A >= BReturn true if A is no less than B otherwise falsebool C = A >= B;  //C is true because 15 is no less than 5.
<=A <= BReturn true if A is no greater than B otherwise falsebool C = A >= B;  //C is false because 15 is greater than 5.
Logical OperatorsSet A=true and B=false
!!ANot 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 & BA and B. It returns true only if both A and B are true otherwise falsebool C = A & B;  //C is false because B is false.
|A | BA or B. It returns false only if both A and B are false otherwise truebool C = A | B;  //C is true because A is true.
^A ^ BA XOR B. It returns true if only one of A or B is true otherwise falsebool C = A ^ B;  //C is true because only A is true.
&&A && BSame as Logical AND but only evaluate B if A is truebool C = A && B;  //C is false because B is false.
||A || BSame as Logical OR but only evaluate B if A is falsebool C = A || B;  //C is true because A is true.
Bitwise OperatorsSet A=1(00000001) and B=2(00000010)
~~AReturns the negation for each bit.sbyte C = ~A;  //C is 11111110.
& A & BReturns binary AND for each bit of the operands.sbyte C = A & B;  //C is 00000000.
|A | BReturns binary OR for each bit of the operands.sbyte C = A | B;  //C is 00000011.
^A ^ BReturns binary XOR for each bit of the operands.sbyte C = A ^ B;  //C is 00000011.
<<A << nReturn n times left shift bit of A.sbyte C = A << 1;  //C 00000010.
>>A >> nReturn n times right shift bit of A.sbyte C = B >> 1;  //C 00000001.
Assignment OperatorsSet A=16 and B=2
=A=BSimple assignment operator. Assign B's value to A.A = B;  //A is 2.
+=A+=BAddition assignment operator. Equivalent to A = A + BA+=B;  //A is 18.
-=A-=BSubtraction assignment operator. Equivalent to A = A - BA-=B;  //A is 14.
*=A*=BMultiplication assignment operator. Equivalent to A = A * BA*=B;  //A is 32.
/=A/=BDivision assignment operator. Equivalent to A = A / BA/=B;  //A is 8.
%=A%=BRemainder assignment operator. Equivalent to A = A % BA%=B;  //A is 0.
&=A&=BAND assignment operator. Equivalent to A = A & BA&=B;  //A is 0.
|=A|=BOR assignment operator. Equivalent to A = A | BA|=B;  //A is 18.
^=A^=BExclusive-OR assignment operator. Equivalent to A = A ^ BA%=B;  //A is 18.
<<=A<<=BLeft shift assignment operator. Equivalent to A = A << BA<<=B;  //A is 64.
>>=A>>=BRight shift assignment operator. Equivalent to A = A >> BA>>=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.