Mastering C# Operator Precedence & Associativity: A Practical Guide
Mastering C# Operator Precedence & Associativity
This in‑depth tutorial explains how the C# compiler evaluates expressions by applying operator precedence and associativity rules. Learn with clear examples and reference tables.
Operator Precedence in C#
Operator precedence defines the order in which parts of an expression are evaluated. Each C# operator has a fixed priority level, ensuring the compiler processes expressions consistently.
For instance, the multiplication operator * has higher precedence than addition +. Therefore, in the expression
int x = 4 + 3 * 5;the multiplication occurs first, yielding
x = 19.
If addition were prioritized over multiplication, the result would be 35—but C# follows the established precedence hierarchy.
Operator Precedence Table
The table below lists operators from highest to lowest precedence.
| Category | Operators |
|---|---|
| Postfix Increment and Decrement | ++, -- |
| Prefix Increment, Decrement and Unary | ++, --, +, -, !, ~ |
| Multiplicative | *, /, % |
| Additive | +, - |
| Shift | <<, >> |
| Relational | <, <=, >, >= |
| Equality | ==, != |
| Bitwise AND | & |
| Bitwise XOR | ^ |
| Bitwise OR | | |
| Logical AND | && |
| Logical OR | || |
| Ternary | ? : |
| Assignment | =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>= |
Assignments have the lowest priority, while postfix increments/decrements sit at the top.
Example 1: Operator Precedence
using System;
namespace OperatorDemo
{
class OperatorPrecedence
{
public static void Main(string[] args)
{
int result1;
int a = 5, b = 6, c = 4;
result1 = --a * b - ++c;
Console.WriteLine(result1);
bool result2;
result2 = b >= c + a;
Console.WriteLine(result2);
}
}
}
Running this program prints:
19 False
Evaluation steps:
(--a)→ 4 (a becomes 4)(++c)→ 5 (c becomes 5)(a * b)→ 24- Subtract: 24 - 5 = 19
The second expression shows that addition + has higher precedence than comparison >=, so c + a is evaluated before the comparison.
Associativity of Operators in C#
When two operators share the same precedence, the compiler uses associativity to decide evaluation order. Most C# operators are left‑to‑right, but a few are right‑to‑left.
Example:
int a = 5, b = 6, c = 3; int result = a * b / c;
Because * and / are left‑to‑right, the multiplication is performed first, yielding 10.
Right‑to‑left associativity is crucial in assignment chains:
int a = 5, b = 6, c = 3; a = b = c;
The value 3 is assigned to b, then b to a, resulting in all variables holding 3.
The table below summarizes C# operator associativity.
| Category | Operators | Associativity |
|---|---|---|
| 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 |
Key right‑to‑left operators include unary prefixes, the ternary operator, and all assignments.
Example 2: Associativity in Action
using System;
namespace OperatorDemo
{
class OperatorAssociativity
{
public static void Main(string[] args)
{
int a = 5, b = 6, c = 3;
int result = a * b / c;
Console.WriteLine(result);
a = b = c;
Console.WriteLine($"a = {a}, b = {b}, c = {c}");
}
}
}
The output is:
10 a = 3, b = 3, c = 3
C Language
- C# Operators – Comprehensive Guide to Operators in C#
- C# Ternary Operator: A Concise Guide to Conditional Expressions
- Mastering C# While and Do‑While Loops: Syntax, Examples, and Best Practices
- Master C++ Operator Overloading: Practical Examples & Best Practices
- Mastering While and Do‑While Loops in C: Practical Examples
- C++ Operators Explained: Types, Examples, and Sample Programs
- Understanding C Constants and Literals: Types, Usage, and Best Practices
- Mastering C++ Overloading: Functions & Operators Explained
- Mastering C# Constants & Literals: Types, Rules, and Best Practices
- Proven Excavator Operator Strategies for Efficient Trenching & Loading