C# Reference ยท Operators and Expressions
C# operator precedence and associativity
C# operator precedence and associativity from highest to lowest, with examples showing why a + b * c, ?? chains and a ? b : c nest the way they do.
When an expression has several operators, precedence decides which one is applied first, and associativity decides the order among operators of the same level. 1 + 2 * 3 is 7 because * binds tighter than +. For operators at the same level, most go left to right: 10 - 4 - 3 is (10 - 4) - 3. Assignment, the conditional operator and ?? go right to left.
Parentheses always win. When a reader has to stop and think about precedence, add them.
int r = 2 + 3 * 4; // 14 int s = (2 + 3) * 4; // 20
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
From highest to lowest precedence:
| Level | Operators | Associativity |
|---|---|---|
| Primary | x.y, x?.y, f(x), a[i], a?[i], x++, x--, x!, new, typeof, default, checked, unchecked, nameof, stackalloc |
left to right |
| Unary | +x, -x, !x, ~x, ++x, --x, ^x, (T)x, await, &x, *x |
right to left |
| Range | x..y |
left to right |
switch, with |
x switch {...}, x with {...} |
left to right |
| Multiplicative | *, /, % |
left to right |
| Additive | +, - |
left to right |
| Shift | <<, >>, >>> |
left to right |
| Relational and type | <, >, <=, >=, is, as |
left to right |
| Equality | ==, != |
left to right |
| Bitwise AND | & |
left to right |
| Bitwise XOR | ^ |
left to right |
| Bitwise OR | | |
left to right |
| Conditional AND | && |
left to right |
| Conditional OR | || |
left to right |
| Null-coalescing | ?? |
right to left |
| Conditional | ?: |
right to left |
| Assignment and lambda | =, +=, -=, *=, ??=, =>, and the other compound forms |
right to left |
Usage notes
- Precedence says how operands are grouped, not the order in which they are evaluated. C# evaluates operands left to right, so
f() + g()always callsffirst.i++ + ++iis well defined in C# (unlike C), and gives1 + 3fori = 1. - A classic trap:
==binds tighter than&,^and|. Soflags & mask == maskmeansflags & (mask == mask), which does not compile for integers. Write(flags & mask) == mask. &&binds tighter than||. Parenthesize mixed conditions:a || (b && c).??has lower precedence than==and+. Inx ?? y + 1the addition happens first.- String concatenation is left to right, so
1 + 2 + "a"is"3a", while"a" + 1 + 2is"a12". - Casts bind tighter than multiplication:
(int)2.9 * 2casts first. Use(int)(2.9 * 2)to cast the product. x is A || x is Bis fine, butx is not A && x is not Bis easier to misread thanx is not (A or B).- The conditional operator is right-associative, so chains read like
if/else if. Nest in parentheses when the branches are complex.