➕ C# Operator Precedence – Understand the Order of Evaluation
🧲 Introduction – Why Operator Precedence Matters
In C#, expressions can contain multiple operators, and operator precedence determines the order in which those operators are evaluated. Understanding precedence helps prevent logical errors and ensures expressions produce the correct results.
🎯 In this guide, you’ll learn:
- How C# decides which operators run first
- Associativity rules for same-precedence operators
- Precedence examples and tables
- Best practices using parentheses
🔍 Core Concept – What Is Operator Precedence?
Operator precedence defines which operations are performed first in a compound expression. Higher precedence operators are evaluated before lower precedence ones.
Example:
int result = 10 + 5 * 2;
Console.WriteLine(result); // Output: 20 (not 30)
🧠 Why? Because *
has higher precedence than +
.
🧾 Operator Precedence Table (Top to Bottom)
Precedence Level | Operators | Associativity |
---|---|---|
1 (Highest) | () (parentheses), x++ , x-- | Left to Right |
2 | +x , -x , !x , ~x , ++x , --x | Right to Left |
3 | * , / , % | Left to Right |
4 | + , - | Left to Right |
5 | << , >> | Left to Right |
6 | < , <= , > , >= | Left to Right |
7 | == , != | Left to Right |
8 | & | Left to Right |
9 | ^ | Left to Right |
10 | ` | ` |
11 | && | Left to Right |
12 | ` | |
13 | ?: (ternary) | Right to Left |
14 | = , += , -= , *= , /= , %= … | Right to Left |
💻 Code Example – Operator Precedence in Action
int a = 10, b = 5, c = 2;
int result = a + b * c; // Multiplies before addition
Console.WriteLine(result); // Output: 20
📘 Equivalent to: a + (b * c)
🔄 Parentheses Override Precedence
int x = (10 + 5) * 2; // Forces addition first
Console.WriteLine(x); // Output: 30
✅ Use parentheses ()
to explicitly control evaluation order and improve code clarity.
🧠 Associativity – Handling Same Precedence
If multiple operators of the same precedence level appear, associativity defines the direction of evaluation.
Left-to-Right Example:
int x = 20 / 5 * 2; // Interpreted as (20 / 5) * 2 = 8
Right-to-Left Example:
int x = y = z = 5; // Evaluated as y = (z = 5)
💡 Tips, Pitfalls & Best Practices
💡 Tip: When in doubt, use parentheses to enforce your desired evaluation.
⚠️ Pitfall: Misinterpreting precedence can cause subtle bugs, especially in compound conditions and arithmetic expressions.
📘 Best Practice: Don’t rely on precedence knowledge alone—write clear and explicit expressions with parentheses.
📌 Summary – Recap & Next Steps
Understanding operator precedence is key to controlling how expressions are evaluated. It ensures your logic runs in the correct sequence and avoids unexpected results.
🔍 Key Takeaways:
- Operators have different levels of precedence
- Associativity defines evaluation direction for same-level operators
- Use parentheses to make complex expressions clearer and safer
⚙️ Up next: Discover how to customize expression behavior using ➕ C# Operator Overloading.
❓ FAQ – C# Operator Precedence
❓ What happens when multiple operators are in one expression?
✅ Operators are evaluated based on precedence, from highest to lowest.
❓ Does C# always follow left-to-right evaluation?
❌ Not always. Associativity defines the direction. Assignment and ternary are right-to-left.
❓ Can parentheses override precedence?
✅ Yes. Use ()
to group and explicitly prioritize operations.
❓ Is precedence the same for all operators?
❌ No. Arithmetic, logical, and assignment operators have different precedence levels.
❓ Should I memorize the precedence table?
🧠 It’s helpful, but not necessary. Use parentheses for clarity and avoid ambiguity.
Share Now :