Operator Precedence and Associativity
Understand evaluation order and use parentheses.
Operator Precedence and Associativity is a free C# Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Precedence Matters
When an expression mixes operators, C# decides the order using precedence. Higher-precedence operators bind tighter and run first.
using System;
class Program {
static void Main() {
Console.WriteLine(2 + 3 * 4);
}
}Multiplicative Before Additive
* / % have higher precedence than + -, so they execute first, just like in math class.
using System;
class Program {
static void Main() {
Console.WriteLine(10 - 2 * 3);
Console.WriteLine(20 / 4 + 1);
}
}Parentheses Override Order
Parentheses have the highest precedence. Use them to force the order you want and to make intent clear.
using System;
class Program {
static void Main() {
Console.WriteLine((2 + 3) * 4);
Console.WriteLine(2 + (3 * 4));
}
}Comparison vs Arithmetic
Arithmetic runs before comparison. In a + b > c, the addition happens first, then the comparison.
using System;
class Program {
static void Main() {
int a = 3, b = 4, c = 5;
Console.WriteLine(a + b > c);
}
}Logical Operator Order
&& has higher precedence than ||. In a || b && c, the AND binds first.
using System;
class Program {
static void Main() {
bool r = true || false && false;
Console.WriteLine(r);
}
}A Precedence Snapshot
Rough order, highest to lowest: parentheses, unary (! ~ -), * / %, + -, shifts, comparisons, equality, bitwise, &&, ||, then assignment.
using System;
class Program {
static void Main() {
int x = 1 + 2 << 1;
Console.WriteLine(x);
}
}Associativity: Left to Right
When operators share precedence, associativity decides direction. Most binary operators are left-associative: 10 - 4 - 2 is (10 - 4) - 2.
using System;
class Program {
static void Main() {
Console.WriteLine(10 - 4 - 2);
Console.WriteLine(100 / 5 / 2);
}
}Right-Associative Assignment
Assignment is right-associative. a = b = 5 assigns 5 to b first, then that result to a.
using System;
class Program {
static void Main() {
int a, b;
a = b = 5;
Console.WriteLine(a + " " + b);
}
}Unary Operators Bind Tightly
Unary !, ~, and negation apply before binary operators. -3 + 2 negates 3 first.
using System;
class Program {
static void Main() {
Console.WriteLine(-3 + 2);
Console.WriteLine(!false && true);
}
}The Conditional Operator
The ternary ?: has low precedence, just above assignment. Parenthesize complex conditions to stay safe.
using System;
class Program {
static void Main() {
int n = 7;
string s = n % 2 == 0 ? "even" : "odd";
Console.WriteLine(s);
}
}Clarity Beats Cleverness
Even when precedence makes parentheses optional, adding them often makes code easier to read and review.
using System;
class Program {
static void Main() {
int a = 2, b = 3, c = 4;
int result = (a * b) + (c * 2);
Console.WriteLine(result);
}
}Quick Check
Test your understanding of precedence and associativity.
Recap
Precedence sets which operators run first; * and / outrank + and -, and parentheses outrank everything. Associativity breaks ties among equal-precedence operators: most go left to right, while assignment goes right to left.
When in doubt, add parentheses for clarity.
Frequently asked questions
Is the “Operator Precedence and Associativity” lesson free?
Yes — the full text of “Operator Precedence and Associativity” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Operator Precedence and Associativity”?
Understand evaluation order and use parentheses. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Operator Precedence and Associativity” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Arithmetic and Assignment Operators
- Comparison and Logical Operators
- Bitwise and Shift Operators
- Operator Precedence and Associativity