
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# - Operators Precedence
Operator precedence determines the grouping of terms in an expression. This affects evaluation of an expression. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator.
For example x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher precedence than +, so the first evaluation takes place for 3*2 and then 7 is added into it.
C# Operator Precedence Table
Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators are evaluated first.
Category | Operator | Associativity |
---|---|---|
Postfix | () [] -> . ++ - - | Left to right |
Unary | + - ! ~ ++ - - (type)* & sizeof | 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 |
Conditional | ?: | Right to left |
Assignment | = += -= *= /= %=>>= <<= &= ^= |= | Right to left |
Comma | , | Left to right |
C# Operators Precedence Levels
The following tables shows the levels of the C# operators precedence:
Precedence Level | Operators | Associativity |
---|---|---|
Highest (1) | () [] . | Left to Right |
2 | ++ -- (postfix) | Left to Right |
3 | ++ -- + - (prefix) ! ~ | Right to Left |
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 | ^ | Left to Right |
11 | | | Left to Right |
12 | && | Left to Right |
13 | || | Left to Right |
14 | ?: (Ternary) | Right to Left |
15 | = += -= *= /= %= &= ^= <<= >>= | Right to Left |
Lowest (16) | , (comma operator) | Left to Right |
Example of Operator Precedence
This example demonstrates how an expression (containing multiple operators) evaluates the operators based on the operators precedence:
using System; namespace OperatorsAppl { class Program { static void Main(string[] args) { int a = 20; int b = 10; int c = 15; int d = 5; int e; e = (a + b) * c / d; // ( 30 * 15 ) / 5 Console.WriteLine("Value of (a + b) * c / d is : {0}", e); e = ((a + b) * c) / d; // (30 * 15 ) / 5 Console.WriteLine("Value of ((a + b) * c) / d is : {0}", e); e = (a + b) * (c / d); // (30) * (15/5) {0}", e); e = a + (b * c) / d; // 20 + (150/5) {0}", e); Console.ReadLine(); } } }
When the above code is compiled and executed, it produces the following result −
Value of (a + b) * c / d is : 90 Value of ((a + b) * c) / d is : 90
More Examples on C# Operator Precedence
Example 1: Multiplication Before Addition
In this example, multiplication has higher precedence than addition.
using System; class Program { static void Main() { int result = 10 + 5 * 2; Console.WriteLine("Result: " + result); } }
When the above code is compiled and executed, it produces the following result −
Result: 20
Example 2: Using Parentheses to Change Precedence
In this example, parentheses alter the precedence of operations.
using System; class Program { static void Main() { int result = (10 + 5) * 2; Console.WriteLine("Result: " + result); } }
When the above code is compiled and executed, it produces the following result −
Result: 30
Example 3: Combining Logical and Relational Operators
In this example, relational operators execute before logical AND.
using System; class Program { static void Main() { bool result = 5 > 3 && 10When the above code is compiled and executed, it produces the following result −
Result: TrueC# Operator Associativity
Operators associatively determine the execution order when an expression has multiple operators having the same precedence.
Operator Associativity Table
Associativity Execution Order Example Left to Right Left operand first a - b + c (a - b) + c Right to Left Right operand first a = b = c a = (b = c) Example of Operators Associativity
In this example, assignment operators execute from right to left.
using System; class Program { static void Main() { int a, b, c; a = b = c = 10; // Right to left assignment Console.WriteLine("a: " + a + ", b: " + b + ", c: " + c); } }When the above code is compiled and executed, it produces the following result −
a: 10, b: 10, c: 10Frequently Asked Questions (FAQ)
Q1: Which operator has the highest precedence in C#?
Operators like parentheses
()
, array indexing[]
, and the dot.
operator have the highest precedence.Q2: What happens when two operators have the same precedence?
If two operators have the same precedence, the associativity rule decides the order. For example,
*
and/
are evaluated from left to right.Q3: How can I avoid confusion with operator precedence?
The best way to avoid mistakes is to use parentheses
()
to make the order of execution clear.