## #28 – Operator Precedence

July 15, 2010 3 Comments

Each operator has an associated precedence, which indicates the order in which the operators are evaluated when evaluating the expression.

When an expression has operators of the same precedence, they are evaluated according to their rules of associativity–left to right for all binary expressions, except for assignment and conditional operators, which are right to left.

Here are the C# operators, in order of precedence. Operators in each group are of equal precedence.

- Primary: x.y f(x) a[x] x++ x– new typeof checked unchecked
- Unary: + – ! ~ ++x –x (T)x
- Multiplicative: * / %
- Additive: + –
- Shift: << >>
- Relational: < > <= >= is as
- Equality: == !=
- Logical AND: &
- Logical XOR: ^
- Logical OR: |
- Conditional AND: &&
- Conditional OR: ||
- Conditional: ?:
- Assignment: = *= /= %= += -= <<= >>= &= ^= |=

Precedence can be changed by using parentheses, the inner expressions being evaluated first.