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# can be 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, which we describe later: 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 their 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 and lambda operators,
null coalescing operator, and (ternary) 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) | |||
| Member access |
| No |
| Pointer to struct (unsafe) |
| No |
| Function call |
| No |
| Array/index |
| Via indexer |
| Post-increment |
| Yes |
| Post-decrement |
| Yes |
| Create instance |
| No |
| Unsafe stack allocation |
| No |
| Get type from identifier |
| No |
|
| No | |
| Integral overflow check off |
| No |
| Default value |
| No |
| Await |
| No |
Unary | |||
| Get size of struct |
| No |
| Positive value of |
| Yes |
| Negative value of |
| Yes |
| Not |
| Yes |
| Bitwise complement |
| Yes |
| Pre-increment |
| Yes |
| Post-increment |
| Yes |
| Cast |
| No |
| Value at address (unsafe) |
| No |
| Address of value (unsafe) |
| No |
Multiplicative | |||
| Multiply |
| Yes |
| Divide |
| Yes |
| Remainder |
| Yes |
Additive | |||
| Add |
| Yes |
| Subtract |
| Yes |
Shift | |||
| Shift left |
| Yes |
| Shift right |
| Yes |
Relational | |||
| Less than |
| Yes |
| Greater than |
| Yes |
| Less than or equal to |
| Yes |
| Greater than or equal to |
| Yes |
| Type is or is subclass of |
| No |
| Type conversion |
| No |
Equality | |||
| Equals |
| Yes |
| Not equals |
| Yes |
Logical And | |||
| And |
| Yes |
Logical Xor | |||
| Exclusive Or |
| Yes |
Logical Or | |||
| Or |
| Yes |
Conditional And | |||
| Conditional And |
| Via |
Conditional Or | |||
| Conditional Or |
| Via |
Conditional (Ternary) | |||
| Conditional |
| No |
Assignment and lambda (lowest precedence) | |||
| Assign |
| 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 |
|