Skip to content
Tutorials Center
My learning

Topics

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.

C#
int r = 2 + 3 * 4;      // 14
int s = (2 + 3) * 4;    // 20

Try it

C# Precedence in arithmetic and logic

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.

C# Right-associative operators and the conditional

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 calls f first. i++ + ++i is well defined in C# (unlike C), and gives 1 + 3 for i = 1.
  • A classic trap: == binds tighter than &, ^ and |. So flags & mask == mask means flags & (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 +. In x ?? y + 1 the addition happens first.
  • String concatenation is left to right, so 1 + 2 + "a" is "3a", while "a" + 1 + 2 is "a12".
  • Casts bind tighter than multiplication: (int)2.9 * 2 casts first. Use (int)(2.9 * 2) to cast the product.
  • x is A || x is B is fine, but x is not A && x is not B is easier to misread than x 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.