In C#, an operator is a term or a symbol that takes one or more expressions, or operands, as input and returns a value. 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:
The following C# statement contains two binary operators, each with two operands. The assignment operator, =, has the integer y and the expression 2 + 3 as operands. The expression 2 + 3 itself contains the addition operator, and has the integer values 2 and 3 as operands:
An operand can be a valid expression of any size, and it can be composed of any number of other operations. When an expression is evaluated, the compiler first evaluates all the operands from left to right. After all operands are evaluated, the operators are evaluated in a specific order known as operator precedence. The following table divides the operators into categories based on the type of operation they perform. The categories are listed in order of precedence.
Primary Operators
Unary Operators
Expression | Description |
|---|
+x | Identity |
-x | Negation |
!x | Logical negation |
~x | Bitwise negation |
++x | Pre-increment |
--x | Pre-decrement |
(T)x | Explicitly convert x to type T |
Multiplicative Operators
Expression | Description |
|---|
* | Multiplication |
/ | Division |
% | Remainder |
Additive Operators
Expression | Description |
|---|
x + y | Addition, string concatenation, delegate combination |
x - y | Subtraction, delegate removal |
Shift Operators
Expression | Description |
|---|
x << y | Shift left |
x >> y | Shift right |
Relational and Type Operators
Expression | Description |
|---|
x < y | Less than |
x > y | Greater than |
x <= y | Less than or equal |
x >= y | Greater than or equal |
x is T | Return true if x is a T, false otherwise |
x as T | Return x typed as T, or null if x is not a T |
Equality Operators
Expression | Description |
|---|
x == y | Equal |
x != y | Not equal |
Assignment and Anonymous Operators
Expression | Description |
|---|
= | Assignment |
x op= y | Compound assignment, supports these operators: +=, -=, *=, /=, %=, &=, |=, !=, <<=, >>= |
(T x) => y | Anonymous function (lambda expression) |
Logical, Conditional, and Null Operators
Category | Expression | Description |
|---|
Logical AND | x & y | Integer bitwise AND, Boolean logical AND |
Logical XOR | x ^ y | Integer bitwise XOR, boolean logical XOR |
Logical OR | x | y | Integer bitwise OR, boolean logical OR |
Conditional AND | x && y | Evaluates y only if x is true |
Conditional OR | x || y | Evaluates y only if x is false |
Null coalescing | X ?? y | Evaluates to y if x is null, to x otherwise |
Conditional | x ?: y : z | Evaluates to y if x is true, z if x is false |
When two 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. The assignment operators and the ternary operator (?:) are right associative. All other binary operators are left associative.
Parentheses can be used to enclose an expression and force that expression to be evaluated before others. For example, 2 + 3 * 2 would ordinarily become 8. This is because multiplicative operators take precedence over additive operators. Writing the expression as (2 + 3) * 2 results in 10, because it indicates to the C# compiler that the addition operator (+) must be evaluated before the multiplication operator (*).
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 (C# Programming Guide).