Operators

There are a large number of operators in C#.
link - learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/


Operator 
Compare object reference typex is Class1
Assignment=
Concatenation+
Concatenation Self Assignment+=
  
Equality==
Equality - Not equal!=
  
Logical AND&
Logical AND Self Assignment&=
Logical AND Conditional&&
Logical OR|
Logical OR Self Assignment|=
Logical OR Conditional||
Logical OR Exclusive^
Logical OR Exclusive Self Assignment^=
Logical NOT~
Logical NOT / unary ??!
  
Division (integer)/ (depending on the operands)
Division Assignment (integer)/= (depending on the operands)
Multiplication*
Multiplication Self Assignment*=
Subtraction-
Subtraction Self Assignment-=
Addition+
Addition Self Assignment+=
Modulus%
Modulus (division returning only the remainder)% (eg 10 % 5)
Modulus Self Assignment%=
Exponentiationn/a
  
Bitwise AND&=
Bitwise OR 
Address of& (unsafe mode only)
Type cast(type)
Pointer to member. (Unsafe mode only)
Scope resolution., base
Conditional?: (right to left)
One's complement / unary ??~
Indirection* (unsafe mode only)
unary plus+
Postfix increment++
Postfix decrement 
Left Shift<<
Left Shift Self Assignment<<=
Right Shift>>
Right Shift Self Assignment>>=
Size of typesizeof


Primary



Unary


++variable 
--variable


Multiplication


value1 * value2 
value1 / value2
value1 % value2


Addition


value1 + value2 
value1 - value2


Shift


Logical

It is common to want to test whether two conditions are both true or only one is true or neither is true.


AND

if (oMyObject = null) & (bUpdate == false) 
{
}

The second condition is only checked if the first one is true. This is often referred to as short circuiting.

if (oMyObject = null) && (bUpdate == false) 
{
}

OR

if (oMyObject = null) | (sFullName == "Steven") 
{
}

The second expression is only evaluated if the first expression is true. This is often referred to as short-circuit evaluation.

if (oMyObject = null) || (sFullName == "Steven") 
{
}

Exclusive OR

Exclusive Or. True if only one statement is true.

if (oMyObject = null) ^ (sFullName == "Steven") 
{
}


Relational and Type Testing



Assignment



Lambda

.NET Library > Framework 3.5 > Lambda Expressions


Operator Precedence

The following is a list of the types of operators in order of precedence


OrderSymbolOperator
1^ 
2- 

Primary
Unary
Multiplication
Addition
Shift
Relational and Type Testing
Equality
Self Assignment
Logical
Conditional
Assignment
Lambda


Equality

Are the variables (or objects) equal.


Operator - ==

Can be used to compare both value and reference data types.
For value types this returns true if the values are equal
For the string reference type this compares the contents.
For all other reference types this compares the identity on the heap.

if (svalue1 == svalue2) 
{
}

'VB.Net Equivalent
If (svalue1 = svalue2) Then
End If

Operators - !=

Are the variables different

if (ovalue1 != null) 
{
}

'VB.Net Equivalent
If ( Not (ovalue Is Nothing) ) Then
End If
If (ovalue IsNot Nothing) Then
End If

Equals() Method

Can be used to compare both value and reference data types.
For all types this compares the contents (not the identity on the heap)
Also known as testing for equality without coalescing.

If (svalue1.Equals(svalue2)) 
{
}

You should always use the Equals() method to check reference types for equality


Self Assignment

It is very common to increment a variable and store the result back in the variable itself.


value = value + 3


VB.NET allows you to use shortcut notation known as "self assignment".


value += 1  'value = value + 1 
value -+ 2 'value = value - 2
value *= 3 'value = value * 3
value /= 4 'value = value / 4
value \= 5 'value = value \ 5

You can also use self assignment with strings


string = "some" 
string += "text" 'string = string & "text"

(You cannot use string &- "text"


+= operator returns a value as well as assigning the value


Operator Overloading

VB.Net does not support operator overloading
This permits user-defined operator implementations
You can redefine the function of most built-in operators globally or on a class by class basis
Overloaded operators are implemented as functions


class MyDate 
{
   private m_Day;
   private m_Month;
   private m_Year;

   bool operator == (const myDate mdate);
   bool operator != (const MyDate mdate);
}

bool MyDate :: operator==(const MyDate mdate)
{
   return !(this -> operator == (mdate));
}
bool MyDate :: operator!=(const MyDate mdate)
{
   return (MyDate.m_Day == this.m_Day) &&
             (MyDate.m_Month == this.m_Month) &&
             (MyDate.m_Year == this.m_Year);
}

Modulus

Division returning only the remainder.

% 
% (eg 10 % 5)

Modulus Self Assignment

%= 

sizeof



nameof

This provides access to element names whether they are class names, method names, parameter names or attribute names
There is a new operator called nameof that lets you remove any string literals that represent the names of any members


Conditional Operator

more



© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext