C# - Operators Precedence
C# Operators Precedence
Operator precedence (order of operations) is a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given expression.
For example, multiplication has higher precedence than addition. Thus, the expression 1 + 2 × 3 is interpreted to have the value 1 + (2 × 3) = 7, and not (1 + 2) × 3 = 9. When exponent is used in the expression, it has precedence over both addition and multiplication. Thus 3 + 52 = 28 and 3 × 52 = 75.
Operator Associativity
Operator associativity is the direction in which an expression is evaluated. For example:
int x = 10; int y = 20; //associativity of = operator is //Left to Right, hence x will become 20 x = y;
As the associativity of = is left to right. Hence x is assigned the value of y.
Example:
The example below illustrates the operator precedence in C#.
using System; class MyProgram { static void Main(string[] args) { int retval1, retval2, retval3; //evaluates 5 * 2 first retval1 = 15 - 5 * 2; //above expression is equivalent to retval2 = 15 - (5 * 2); //forcing compiler to evaluate 15 - 5 first retval3 = (15 - 5) * 2; Console.WriteLine("15 - 5 * 2 = " + retval1); Console.WriteLine("15 - (5 * 2) = " + retval2); Console.WriteLine("(15 - 5) * 2 = " + retval3); } }
The output of the above code will be:
15 - 5 * 2 = 5 15 - (5 * 2) = 5 (15 - 5) * 2 = 20
C# Operators Precedence Table
The following table lists the precedence and associativity of C# operators. Operators are listed top to bottom, in descending precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. When operators have the same precedence, associativity of the operators determines the order in which the operations are performed.
Precedence | Operator | Description |
---|---|---|
18 | x++ x-- | Postfix increment, Postfix decrement |
x.y | Member access expression | |
f(x) | Invocation expression | |
a[i] | Indexer operator | |
x?.y | Member access | |
x?[y] | Element access | |
! | ! (null-forgiving) operator | |
new | new operator | |
typeof | typeof operator | |
checked unchecked | checked and unchecked keyword | |
default | default value expressions | |
nameof | nameof expression | |
delegate | delegate operator | |
sizeof | sizeof operator | |
stackalloc | stackalloc expression | |
x->y | Pointer member access operator | |
17 | ++x --x | Prefix increment, Prefix decrement |
+x -x | Unary plus, Unary minus | |
! | Logical NOT | |
~ | Bitwise NOT | |
^x | Index from end operator | |
(T)x | Cast expression | |
await | await operator | |
*x | Indirection (dereference) operator | |
&x | Address-of operator | |
true false | Boolean true and false operator | |
16 | .. | Range operator |
15 | switch | switch expression |
14 | with | with expression |
13 | * / % | Multiplication, Division, Remainder |
12 | + - | Addition, Subtraction |
11 | << >> | Bitwise left shift and right shift |
10 | < <= > >= | Less than, Less than or equal, Greater than, and Greater than or equal |
is as | Type testing operators | |
9 | == != | Equality and Inequality |
8 | & | Boolean logical AND or bitwise logical AND |
7 | ^ | Boolean logical XOR or bitwise logical XOR |
6 | | | Boolean logical OR or bitwise logical OR |
5 | && | Logical AND |
4 | || | Logical OR |
3 | ?? | Null-coalescing operator |
2 | x?a:b | Conditional (ternary) operator |
1 | = | Direct assignment |
+= -= *= /= %= | Compound assignment by sum, difference, product, quotient and remainder | |
<<= >>= | Compound assignment by Bitwise left shift and right shift | |
&= ^= |= | Compound assignment by Bitwise AND, XOR and OR | |
??= | Compound assignment by Null-coalescing operator | |
=> | lambda operator |
Operator associativity
C# has following operator associativity:
- Left-associative operators: Except for the assignment operators and the null-coalescing operators, all binary operators are left-associative.
- Right-associative operators: The assignment operators, the null-coalescing operators, and the conditional operator ?: are right-associative.
Parentheses can be used to change the order of evaluation imposed by operator associativity.
❮ C# - Operators