Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Mastering C# Operator Precedence & Associativity: A Practical Guide

Mastering C# Operator Precedence & Associativity

This in‑depth tutorial explains how the C# compiler evaluates expressions by applying operator precedence and associativity rules. Learn with clear examples and reference tables.

Operator Precedence in C#

Operator precedence defines the order in which parts of an expression are evaluated. Each C# operator has a fixed priority level, ensuring the compiler processes expressions consistently.

For instance, the multiplication operator * has higher precedence than addition +. Therefore, in the expression

int x = 4 + 3 * 5;
the multiplication occurs first, yielding x = 19.

If addition were prioritized over multiplication, the result would be 35—but C# follows the established precedence hierarchy.


Operator Precedence Table

The table below lists operators from highest to lowest precedence.

C# Operator Precedence
CategoryOperators
Postfix Increment and Decrement++, --
Prefix Increment, Decrement and Unary++, --, +, -, !, ~
Multiplicative*, /, %
Additive+, -
Shift<<, >>
Relational<, <=, >, >=
Equality==, !=
Bitwise AND&
Bitwise XOR^
Bitwise OR|
Logical AND&&
Logical OR||
Ternary? :
Assignment=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=

Assignments have the lowest priority, while postfix increments/decrements sit at the top.


Example 1: Operator Precedence

using System;

namespace OperatorDemo
{
    class OperatorPrecedence
    {
        public static void Main(string[] args)
        {
            int result1;
            int a = 5, b = 6, c = 4;
            result1 = --a * b - ++c;
            Console.WriteLine(result1);

            bool result2;
            result2 = b >= c + a;
            Console.WriteLine(result2);
        }
    }
}

Running this program prints:

19
False

Evaluation steps:

The second expression shows that addition + has higher precedence than comparison >=, so c + a is evaluated before the comparison.


Associativity of Operators in C#

When two operators share the same precedence, the compiler uses associativity to decide evaluation order. Most C# operators are left‑to‑right, but a few are right‑to‑left.

Example:

int a = 5, b = 6, c = 3;
int result = a * b / c;

Because * and / are left‑to‑right, the multiplication is performed first, yielding 10.

Right‑to‑left associativity is crucial in assignment chains:

int a = 5, b = 6, c = 3;
a = b = c;

The value 3 is assigned to b, then b to a, resulting in all variables holding 3.

The table below summarizes C# operator associativity.

C# Operator Associativity
CategoryOperatorsAssociativity
Postfix Increment and Decrement++, --Left to Right
Prefix Increment, Decrement and Unary++, --, +, -, !, ~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
Ternary? :Right to Left
Assignment=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=Right to Left

Key right‑to‑left operators include unary prefixes, the ternary operator, and all assignments.


Example 2: Associativity in Action

using System;

namespace OperatorDemo
{
    class OperatorAssociativity
    {
        public static void Main(string[] args)
        {
            int a = 5, b = 6, c = 3;
            int result = a * b / c;
            Console.WriteLine(result);

            a = b = c;
            Console.WriteLine($"a = {a}, b = {b}, c = {c}");
        }
    }
}

The output is:

10
a = 3, b = 3, c = 3

C Language

  1. C# Operators – Comprehensive Guide to Operators in C#
  2. C# Ternary Operator: A Concise Guide to Conditional Expressions
  3. Mastering C# While and Do‑While Loops: Syntax, Examples, and Best Practices
  4. Master C++ Operator Overloading: Practical Examples & Best Practices
  5. Mastering While and Do‑While Loops in C: Practical Examples
  6. C++ Operators Explained: Types, Examples, and Sample Programs
  7. Understanding C Constants and Literals: Types, Usage, and Best Practices
  8. Mastering C++ Overloading: Functions & Operators Explained
  9. Mastering C# Constants & Literals: Types, Rules, and Best Practices
  10. Proven Excavator Operator Strategies for Efficient Trenching & Loading