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 && 10 

When the above code is compiled and executed, it produces the following result −

Result: True

C# 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: 10

Frequently 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.

Advertisements