Operator Precedence and Associativity in C# Programming

Operator Precedence is ordering of operators according of its priority. Each operator have different priority level. For example, in a expression a = b + c / d; , operator '/' has the highest precedence and operator '=' has the lowest precedence. So, '/' will be operated first, '+' will be operated after that and lastly '=' will be operated.

There are some operators whose precedence is same. If such operators are in same expression, then it will be evaluated according to its associativity. It can either be from left or from right. This is known as Operator Associativity. For example, in a expression a = b + c - d; , operator '+' and '-' have same precedence, so, it will be evaluated according to its accociativity which is left to right in case of the additive associativity, So, operator '+' will be operated before '-'.

Operators according to its precedence (Highest priority to lowest priority) with their associativity is given below:

  1. Primary Operators
  2. Unary Operators
  3. Multiplicative Operators
  4. Additive Operators
  5. Shift Operators
  6. Relational and Type Operators
  7. Equality Operators
  8. Bitwise Operators
  9. Logical Operators
  10. Conditional Operator
  11. Assignment Operator

 

1. Primary Operators

Primary Operators have the highest precedence in C# programming. They have left to right associativity. Some of these operators are ., ->, ( ), [ ], ++ (as postfix), -- (as postfix), new, typeof, default, delegate, checked, unchecked etc.

2. Unary Operators

Unary Operators are operators that work only with one operand. These operators have right to left associativity. Some of there operators are + (unary), - (unary), !, ~, ++ (prefix), -- (prefix) etc.

3. Multiplicative Operators

Multiplicative Operators work with multiplication or division. They have left to right associativity. Examples of these operators are *, / and %.

4. Additive Operators


Additive Operators work with addition or subtraction. They have left to right associativity. Examples of these operators are + and -. These operators can also be used for delegate combination or removal and '+' can also be used for string concatenation.

5. Shift Operators

Shift Operators are used to perform bitwise shift. They work with binary values. They have left to right associativity. Examples of these operators are <<, >>.

6. Relational and Type Operators

Relational and Type operators are used to comparing and type testing two variables.They have left to right associativity. Examples of relational and type operators are <, <=, >, >=, is and as.

7. Equality Operators

Equality Operators are used to check if two operands are equal or not. They have left to right associativity. Examples of equality operators are == and !=.

8. Bitwise Operators

Bitwise Operators works in bit level and returns result according to boolean result. They have left to right associativity. There are three bitwise operators and each of them have different priority level. They are listed below according to their precedence:

  • Bitwise AND : &
  • Bitwise XOR : ^
  • Bitwise OR : |

9. Logical Operators

Logical Operators compares two operands and returns result according to boolean result. They have left to right associativity. There are two logical operators and both of them have different priority level. They are listed below according to their precedence:

  • Logical AND : &&
  • Logical OR : ||

10. Conditional Operator

Conditional Operator selects a value between two values according to boolean result of a condition. They have right to left associativity. '?:' is conditional operator.

11. Assignment Operator

Assignment Operator is used to store a value to a variable from right operand to left. They have right to left associativity. Examples of assignment operators are = and any compound assignments (+=, -=, /=, *=,......) etc.

Precedence and Associativity of Operators in C# in Order of Priority 
Operators Associativity
Primary Operators
., ->, ( ), [ ], ++ (as postfix), -- (as postfix), new, typeof, default, delegate, checked, unchecked
Left to Right
Unary Operators
+ (unary), - (unary), !, ~, ++ (prefix), -- (prefix)
Right to left
Multiplicative Operators
*, / and %
Left to Right
Additive Operators
+ and -
Left to Right
Shift Operators
<<, >>
Left to Right
Relational and Type operators
<, <=, >, >=, is and as
Left to Right
Equality Operators
== and !=
Left to Right
Bitwise AND Operator
&
Left to Right
Bitwise XOR Operator
^
Left to Right
Bitwise OR Operator
|
Left to Right
Logical AND Operator
&&
Left to Right
Logical OR Operator
||
Left to Right
Conditional Operator
?:
Right to Left
Assignment Operator
= and any compound assignments (+=, -=, /=, *=,.......)
Right to Left