Operator precedence tells the computer which part of a math or logic expression to do first. Evaluation order is the sequence in which the computer calculates each part.
0
0
Operator precedence and evaluation order in C Sharp (C#)
Introduction
When you write math expressions with multiple operators like +, -, *, /.
When you combine logical conditions using &&, || in if statements.
When you want to understand why a complex expression gives a certain result.
When debugging code that uses many operators together.
When you want to write clear code without extra parentheses.
Syntax
C Sharp (C#)
expression = operand operator operand // Example: int result = 3 + 4 * 2;
Operators like * and / have higher precedence than + and -.
Parentheses () can change the order by forcing parts to be evaluated first.
Examples
Multiplication (*) happens before addition (+), so 4*2=8, then 3+8=11.
C Sharp (C#)
int result = 3 + 4 * 2; // result is 11
Parentheses force addition first: 3+4=7, then 7*2=14.
C Sharp (C#)
int result = (3 + 4) * 2; // result is 14
Logical AND (&&) has higher precedence than OR (||), so false && false is false, then true || false is true.
C Sharp (C#)
bool check = true || false && false; // check is true
Parentheses change order: true || false is true, then true && false is false.
C Sharp (C#)
bool check = (true || false) && false; // check is false
Sample Program
This program shows how operator precedence and parentheses affect the results of math and logical expressions.
C Sharp (C#)
using System; class Program { static void Main() { int a = 5 + 3 * 2; int b = (5 + 3) * 2; bool c = true || false && false; bool d = (true || false) && false; Console.WriteLine($"a = {a}"); Console.WriteLine($"b = {b}"); Console.WriteLine($"c = {c}"); Console.WriteLine($"d = {d}"); } }
OutputSuccess
Important Notes
Remember that operators with higher precedence run before those with lower precedence.
Use parentheses to make your code easier to read and to control evaluation order.
Evaluation order matters when expressions have side effects like method calls.
Summary
Operator precedence decides which operations happen first in an expression.
Parentheses can change the normal precedence to control evaluation order.
Understanding this helps avoid bugs and write clearer code.