Operator Precedence and Associativity

When expression contains multiple operators then precedence and associativity decide the evaluation order.

BODMAS is the order of precedence in general:

  • B – Brackets first
  • O – Of (orders: powers and square roots, cube roots, etc)
  • D – Division (left to right)
  • M – Multiplication (left to right)
  • A – Addition (left to right)
  • S – Subtraction (left to right)

Left-associative operators

Binary operators (except for assignment, lambda, and null-coalescing operators) are
left-associative; in other words, they are evaluated from left to right.

8/4/2 = (8/4)/2

Right-associative operators

The assignment operators, lambda, null-coalescing, and conditional operator are
right-associative; in other words, they are evaluated from right to left.

x = y = 3

It first assigns 3 to y and then to x

Null Operators

  1. Null-Coalescing Operator (??)
    If the left-hand expression is non-null right-hand expression is never evaluated.
  2. Null-conditional operator (?.)
    Allows to call methods and access members

Leave a Reply

Your email address will not be published. Required fields are marked *