Operator precedence defines the order in which different operators are evaluated in an expression without parentheses.
Operators with higher precedence are evaluated before operators with lower precedence. If two operators have the same precedence, their evaluation is determined by the operator’s associativity (left to right or right to left).
If you want to learn more about Operator Precedence
Operator Precedence Table
In C#, operators are grouped into levels of precedence. Below is a table that shows some of the most common operators organized by their precedence, from highest to lowest.
Precedence | Operator | Description | Associativity |
---|---|---|---|
1 | () | Parentheses | N/A |
2 | ++ , -- | Increment and Decrement | Postfix |
3 | + , - | Unary Plus and Minus | Right to Left |
4 | * , / , % | Multiplication, Division, Modulo | Left to Right |
5 | + , - | Addition, Subtraction | Left to Right |
6 | << , >> | Bitwise Shift | Left to Right |
7 | < , > , <= , >= | Comparison | Left to Right |
8 | == , != | Equality, Inequality | Left to Right |
9 | & | Bitwise AND | Left to Right |
10 | ^ | Bitwise XOR | Left to Right |
11 | ` | ` | Bitwise OR |
12 | && | Logical AND | Left to Right |
13 | ` | ` | |
14 | ?: | Conditional (Ternary) Operator | Right to Left |
15 | = , += , -= | Assignment and Compound Operators | Right to Left |
How Operator Precedence Works
Evaluating Expressions
Operator precedence determines the order of evaluation in complex expressions. For example, suppose we have the following expression:
int result = 3 + 4 * 2; // equivalent to 3 + (4 * 2) = 11
In this expression, the multiplication (*
) has a higher precedence than the addition (+
). Therefore, 4 * 2
is evaluated first, resulting in 8
, and then 3
is added, resulting in 11
.
Using Parentheses
To change the order of evaluation, we can use our faithful friends, parentheses. Parentheses have the highest precedence and force the evaluation of the expression they contain first.
int result = (3 + 4) * 2;
Here, 3 + 4
is evaluated first due to the parentheses, resulting in 7
, and then multiplied by 2
, resulting in 14
.
Associativity
When operators have the same precedence, associativity determines the order of evaluation. Most operators in C# have left-to-right associativity, meaning they are evaluated from left to right.
However, some operators, like assignment operators, have right-to-left associativity.
int a = 1;
int b = 2;
int c = 3;
int result = a + b - c;
In this example, both +
and -
have the same precedence and left-to-right associativity. Therefore, a + b
is evaluated first, resulting in 3
, and then 3 - c
is evaluated, resulting in 0
.
Complex Example
Let’s consider a more complex expression to illustrate how precedence and associativity combine:
int result = 5 + 2 * 3 - 4 / 2 + (1 + 3);
We evaluate step by step according to precedence and associativity:
- Evaluate the expression in parentheses:
(1 + 3)
, resulting in4
. - Evaluate the multiplication:
2 * 3
, resulting in6
. - Evaluate the division:
4 / 2
, resulting in2
. - Evaluate the remaining operations from left to right:
5 + 6
results in11
.11 - 2
results in9
.9 + 4
results in13
.
Therefore, the final result of the expression is 13
.
Best Practices Tips
Although operator precedence allows for omitting parentheses in many situations, it’s a good practice to use parentheses to improve code clarity. This not only makes the code easier to understand but also reduces the likelihood of errors.
int result = (5 + 2) * (3 - 1);
Understanding Compound Operators
Compound operators, like +=
and -=
, combine assignment with another operation. These operators have lower precedence, which can affect the results of expressions if not understood correctly.
int a = 5;
a += 3 * 2; // a = a + (3 * 2)
Console.WriteLine(a); // Output: 11
In this example, 3 * 2
is evaluated first due to its higher precedence, then the result 6
is added to a
.