When operators have the same precedence, the associativity of the operators determines the order in which the operations are performed.
Below are the two associativity of operators in c#:
- Left-associative: The order of execution is from left to right.
- Right-associative: The order of execution is from right to left.
Below is the table that shows the Associativity of Operators in C#.
|Postfix Increment and Decrement||++, —||Left to Right|
|Prefix Increment, Decrement, and Unary||++, –, +, -, !, ~||Right to Left|
|Multiplicative||*, /, %||Left to Right|
|Additive||+, –||Left to Right|
|Shift||<<, >>||Left to Right|
|Relational||<, <=, >, >=||Left to Right|
|Equality||==, !=||Left to Right|
|Bitwise AND||&||Left to Right|
|Bitwise XOR||^||Left to Right|
|Bitwise OR|||||Left to Right|
|Logical AND||&&||Left to Right|
|Logical OR||||||Left to Right|
|Ternary||? :||Right to Left|
|Assignment||=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=||Right to Left|