Expressions are constructed from operands and operators. The operators of an expression indicate which operations to apply to the operands. Examples of operators include
new. Examples of operands include literals, fields, local variables, and expressions.
When an expression contains multiple operators, the precedence of the operators controls the order in which the individual operators are evaluated. For example, the expression
x + y * z is evaluated as
x + (y * z) because the
* operator has higher precedence than the
When an operand occurs between two operators with the same precedence, the associativity of the operators controls the order in which the operations are performed:
- Except for the assignment and null-coalescing operators, all binary operators are left-associative, meaning that operations are performed from left to right. For example,
x + y + zis evaluated as
(x + y) + z.
- The assignment operators, the null-coalescing
??=operators, and the conditional operator
?:are right-associative, meaning that operations are performed from right to left. For example,
x = y = zis evaluated as
x = (y = z).
Precedence and associativity can be controlled using parentheses. For example,
x + y * z first multiplies
z and then adds the result to
(x + y) * z first adds
y and then multiplies the result by
Most operators can be overloaded. Operator overloading permits user-defined operator implementations to be specified for operations where one or both of the operands are of a user-defined class or struct type.
For the complete list of C# operators ordered by precedence level, see C# operators.