# #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.

Pingback: #1,074 – Use Parentheses in Expressions to Make Code More Readable | 2,000 Things You Should Know About C#

Pingback: #1,075 – Operator Precedence Doesn’t Affect Operand Evaluation Order | 2,000 Things You Should Know About C#

Precedence and associativity.

Nice work, Sean. Thank you.