C# Operator Precedence

Operator precedence plays an important role while the execution of an expression which contains multiple operators. Operator precedence specifies that which operator will perform its execution first.

For example, a + b * c evaluation will take place as a + ( b * c) because * operator has a higher precedence than the + operator.

Following is the list operators with their precedence. Operators are depicted in the table according to higher to lower precedence.

# Operator Category Operators List Associativity
1 Postfix () [] -> . ++ - - Left to right
2 Unary + - ! ~ ++ - - (type)* & sizeof Right to left
3 Multiplicative * / % Left to right
4 Additive + - Left to right
5 Shift << >> Left to right
6 Relational < <= > >= Left to right
7 Equality == != Left to right
8 Bitwise AND & Left to right
9 Bitwise XOR ^ Left to right
10 Bitwise OR | Left to right
11 Logical AND && Left to right
12 Logical OR || Left to right
13 Conditional ?: Right to left
14 Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
15 Comma , Left to right

Let's look at these operators with an example.

Example

using System;
 
namespace TutorialsLeader
{
    public class MyFirstClass
    {
        static void Main()
        {
            int x = 100, y = 50, z = 200;
 
            Console.WriteLine("x + y * z = {0}", x + (y * z));  // x + y * z is same as x + (y * z)
            Console.WriteLine("x * y + z = (x * y) + z = {0}", (x * y) + z);  // x * y + z is same as (x * y) + z
 
            Console.WriteLine("x + y * z / x = {0}", x + (y * (z / x)));  // x + y * z / x is same as (x + (y * (z / x)))
            Console.WriteLine("x * y + z = (x * y) + z = {0}", (x * y) + z);  // x * y + z is same as (x * y) + z
        }
    }
}
Output

x + y * z = 10100
x * y + z = (x * y) + z = 5200
x + y * z / x = 200
x * y + z = (x * y) + z = 5200

Let's learn about the C# keywords in the next section.