An expression essentially denotes a value. The simplest kinds of
expressions are constants (such as 123
)
and variables (such as x
). Expressions
can be transformed and combined using operators. An operator takes one or more input
operands to output a new expression:
12 * 30 // * is an operator; 12 and 30 are operands.
Complex expressions can be built because an operand may itself be an
expression, such as the operand (12 *
30)
in the following example:
1 + (12 * 30)
Operators in C# are classed as unary, binary, or ternary—depending on the number of operands they work on (one, two, or three). The binary operators always use infix notation, where the operator is placed between the two operands.
Operators that are intrinsic to the basic plumbing of the language are called primary; an example is the method call operator. An expression that has no value is called a void expression:
Console.WriteLine (1)
Since a void expression has no value, it cannot be used as an operand to build more complex expressions:
1 + Console.WriteLine (1) // Compile-time error
An assignment expression uses the =
operator to assign
the result of another expression to a variable. For example:
x = x * 5
An assignment expression is not a void expression. It actually
carries the assignment value, and so can be incorporated into another
expression. In the following example, the expression assigns 2 to
x
and 10 to y
:
y = 5 * (x = 2)
This style of expression can be used to initialize multiple values:
a = b = c = d = 0
The compound assignment operators are syntactic shortcuts that combine assignment with another operator. For example:
x *= 2 // equivalent to x = x * 2 x <<= 1 // equivalent to x = x << 1
(A subtle exception to this rule is with
events, described further in Event Accessors: the +=
and −=
operators here are treated
specially, and map to the event’s add
and remove
accessors).
When an expression contains multiple operators, precedence and associativity determine the order of evaluation. Operators with higher precedence execute before operators of lower precedence. If the operators have the same precedence, the operator’s associativity determines the order of evaluation.
The expression 1 + 2 * 3
is
evaluated as 1 + (2 * 3)
because
*
has a higher precedence than
+
.
Binary operators (except for assignment, lambda, and null
coalescing operators) are
left-associative; in other words, they are
evaluated from left to right. For example, the expression 8/4/2
is evaluated as (8/4)/2
due to left associativity. Of
course, you can insert your own parentheses to change evaluation
order.
The assignment operators, lambda, null coalescing
and conditional operator are right-associative;
in other words, they are evaluated from right to left. Right
associativity allows multiple assignments such as x=y=3
to compile: it works by first
assigning 3
to y
, and then assigning the result of that
expression (3
) to x
.
The following table lists C#’s operators in order of precedence. Operators listed under the same subheading have the same precedence. We explain user-overloadable operators in the section Operator Overloading.
Operator symbol | Operator name | Example | Overloadable |
---|---|---|---|
Primary (highest precedence) | |||
|
| No | |
|
| No | |
|
| No | |
|
| No | |
|
| Via indexer | |
|
| Yes | |
|
| Yes | |
|
| No | |
|
| No | |
|
| No | |
|
| No | |
|
| No |
Unary | |||
---|---|---|---|
|
| No | |
|
| Yes | |
|
| Yes | |
|
| Yes | |
|
| Yes | |
|
| Yes | |
|
| Yes | |
|
| No | |
|
| No | |
|
| No |
Multiplicative | |||
---|---|---|---|
|
| Yes | |
|
| Yes | |
|
| Yes |
Additive | |||
---|---|---|---|
|
| Yes | |
|
| Yes |
Shift | |||
---|---|---|---|
|
| Yes | |
|
| Yes |
Relational | |||
---|---|---|---|
|
| Yes | |
|
| Yes | |
|
| Yes | |
|
| Yes | |
|
| No | |
|
| No |
Equality | |||
---|---|---|---|
|
| Yes | |
|
| Yes |
Logical And | |||
---|---|---|---|
|
| Yes |
Logical Xor | |||
---|---|---|---|
|
| Yes |
Logical Or | |||
---|---|---|---|
|
| Yes |
Conditional And | |||
---|---|---|---|
|
| Via |
Conditional Or | |||
---|---|---|---|
|
| Via |
Conditional | |||
---|---|---|---|
|
| No |
Assignment | |||
---|---|---|---|
|
| No | |
| Multiply self by |
| Via |
| Divide self by |
| Via |
| Add to self |
| Via |
| Subtract from self |
| Via |
| Shift self left by |
| Via |
| Shift self right by |
| Via |
| And self by |
| Via |
| Exclusive-Or self by |
| Via |
| Or self by |
| Via |
Lambda (lowest precedence) | |||
---|---|---|---|
|
| No |