Operators (C# Programming Guide)

 

Updated: July 20, 2015

In C#, an operator is a program element that is applied to one or more operands in an expression or statement. Operators that take one operand, such as the increment operator (++) or new, are referred to as unary operators. Operators that take two operands, such as arithmetic operators (+,-,*,/), are referred to as binary operators. One operator, the conditional operator (?:), takes three operands and is the sole ternary operator in C#.

The following C# statement contains a single unary operator and a single operand. The increment operator, ++, modifies the value of the operand y.

            y++;

The following C# statement contains two binary operators, each with two operands. The assignment operator, =, has the integer variable y and the expression 2 + 3 as operands. The expression 2 + 3 itself consists of the addition operator and two operands, 2 and 3.

            y = 2 + 3;

An operand can be a valid expression that is composed of any length of code, and it can comprise any number of sub expressions. In an expression that contains multiple operators, the order in which the operators are applied is determined by operator precedence, associativity, and parentheses.

Each operator has a defined precedence. In an expression that contains multiple operators that have different precedence levels, the precedence of the operators determines the order in which the operators are evaluated. For example, the following statement assigns 3 to n1.

n1 = 11 - 2 * 4;

The multiplication is executed first because multiplication takes precedence over subtraction.

The following table separates the operators into categories based on the type of operation they perform. The categories are listed in order of precedence.

Primary Operators

ExpressionDescription
x.y

x?.y
Member access

Conditional member access
f(x)Method and delegate invocation
a[x]

a?[x]
Array and indexer access

Conditional array and indexer access
x++Post-increment
x--Post-decrement
new T(...)Object and delegate creation
new T(...){...}Object creation with initializer. See Object and Collection Initializers.
new {...}Anonymous object initializer. See Anonymous Types.
new T[...]Array creation. See Arrays.
typeof(T)Obtain System.Type object for T
checked(x)Evaluate expression in checked context
unchecked(x)Evaluate expression in unchecked context
default (T)Obtain default value of type T
delegate {}Anonymous function (anonymous method)

Unary Operators

ExpressionDescription
+xIdentity
-xNegation
!xLogical negation
~xBitwise negation
++xPre-increment
--xPre-decrement
(T)xExplicitly convert x to type T

Multiplicative Operators

ExpressionDescription
*Multiplication
/Division
%Remainder

Additive Operators

ExpressionDescription
x + yAddition, string concatenation, delegate combination
x - ySubtraction, delegate removal

Shift Operators

ExpressionDescription
x << yShift left
x >> yShift right

Relational and Type Operators

ExpressionDescription
x < yLess than
x > yGreater than
x <= yLess than or equal
x >= yGreater than or equal
x is TReturn true if x is a T, false otherwise
x as TReturn x typed as T, or null if x is not a T

Equality Operators

ExpressionDescription
x == yEqual
x != yNot equal

Logical, Conditional, and Null Operators

CategoryExpressionDescription
Logical ANDx & yInteger bitwise AND, Boolean logical AND
Logical XORx ^ yInteger bitwise XOR, boolean logical XOR
Logical ORx | yInteger bitwise OR, boolean logical OR
Conditional ANDx && yEvaluates y only if x is true
Conditional ORx || yEvaluates y only if x is false
Null coalescingx ?? yEvaluates to y if x is null, to x otherwise
Conditionalx ? y : zEvaluates to y if x is true, z if x is false

Assignment and Anonymous Operators

ExpressionDescription
=Assignment
x op= yCompound assignment. Supports these operators: +=, -=, *=, /=, %=, &=, |=, !=, <<=, >>=
(T x) => yAnonymous function (lambda expression)

When two or more operators that have the same precedence are present in an expression, they are evaluated based on associativity. Left-associative operators are evaluated in order from left to right. For example, x * y / z is evaluated as (x * y) / z. Right-associative operators are evaluated in order from right to left. For example, the assignment operator is right associative. If it were not, the following code would result in an error.

int a, b, c;  
c = 1;  
// The following two lines are equivalent.  
a = b = c;  
a = (b = c);  
  
// The following line, which forces left associativity, causes an error.  
//(a = b) = c;  

As another example the ternary operator (?:) is right associative. Most binary operators are left associative.

Whether the operators in an expression are left associative or right associative, the operands of each expression are evaluated first, from left to right. The following examples illustrate the order of evaluation of operators and operands.

StatementOrder of evaluation
a = ba, b, =
a = b + ca, b, c, +, =
a = b + c * da, b, c, d, *, +, =
a = b * c + da, b, c, *, d, +, =
a = b - c + da, b, c, -, d, +, =
a += b -= ca, b, c, -=, +=

You can change the order imposed by operator precedence and associativity by using parentheses. For example, 2 + 3 * 2 ordinarily evaluates to 8, because multiplicative operators take precedence over additive operators. However, if you write the expression as (2 + 3) * 2, the addition is evaluated before the multiplication, and the result is 10. The following examples illustrate the order of evaluation in parenthesized expressions. As in previous examples, the operands are evaluated before the operator is applied.

StatementOrder of evaluation
a = (b + c) * da, b, c, +, d, *, =
a = b - (c + d)a, b, c, d, +, -, =
a = (b + c) * (d - e)a, b, c, +, d, e, -, *, =

You can change the behavior of operators for custom classes and structs. This process is referred to as operator overloading. For more information, see Overloadable Operators.

For more information, see Operator Keywords and C# Operators.

C# Programming Guide
Statements, Expressions, and Operators

Show: