Website development and design blog, tutorials and inspiration

C# Operator List

Handy C# Operator list for reference

By , 6th November 2007 in C#

This handy reference c# operator list shows you the operators in C#, some are which are straightforward and will be used in every single program or module, while others are more complex and you may not ever use.
 

This article relates to an old version of the .Net Framework. While the contents are still relevant, future versions of the framework may change, or improve upon, the contents of this article.

The most common of the operators are the arithmetic and logic operators. These will be very familiar to you is you know other programming languages, or mathematics.

Arithmetic

OperatorActionExampleResult
+Additionz = 1 + 2z = 3
-Subtractionz = 1 - 2z = -1
*Multiplicationz = 2 * 2z = 4
/Divisionz = 22 / 7z = 3.142857
%Modulusz = 22 % 7z = 1
The modulus operator (%) computes the remainder after dividing its first operand by its second.

 

Logic

OperatorActionExampleResult
&& Logical ANDtrue && false
true && true
false && false
false
true
false
||Logical ORtrue || false
true || true
false || false
true
true
false
!Logical NOTtrue && !falsetrue

 

Increment and Decrement

OperatorActionExampleResult
++Incrementa=1;
a++;
a = 2
––Decrementa=1;
a––;
a = 0;

 

Relational

OperatorActionExampleResult
==Equalsx = 1;
x == 1
true
!=NOT Equalsx = 1;
x != 1
false
<Less thanx = 1;
x < 2;
true
>Greater thanx = 1;
x > 0;
true
<=Less than or equal tox = 1;
x <= 0
false
>=Greater than or equal tox = 1;
x >= 5
false

 

Assignment

OperatorActionExampleResult
=Assignmentx = 1
+=Incremental Additiona=1;
a += 3;
a = 4;
-=Incremental Decrementa=1;
a -= 3;
a = -2;
*=Multiply bya=2;
a *= 4;
a = 8;
/=Divide bya=8;
a /= 2;
a = 4;
%=Modulus or Remaindera=8;
a %= 3;
a = 2;
&=Logical AND "x &= y" is equivalent to "x = x & y"
|=Logical OR"x |= y" is equivalent to "x = x | y"
<<=Left Shift "x <<= y" is equivalent to "x = x << y"
>>=Right Shift"x >>= y" is equivalent to "x = x >> y"

 

Others


OperatorActionExampleResult
&Logical ANDif (false & ++i == 1)false
|Logical OR true | false
false | false
true
false
^Logical Exclusive XORfalse ^ false
false ^ true
true ^ true
false
true
false
~Bitwise Complement x = ~0x00000000x = 0xffffffff
<<Left Shift1 << 12
>>Right Shift-1000 >> 3-125
??Default Valueint y = x ?? -1;if x = null y = -1 else y = x
:?Conditional Operatorcondition ? expression if true : expression if  false
Comments
  1. Tim
    Tim

    Thanks for the handy reference! I saved it to my bookmarks.
    -Tim

Leave a Reply

Your email address will not be published.