CSharp - Operator Precedence and Associativity

Introduction

The following table lists C#'s operators in order of precedence.

Operators in the same category have the same precedence.

Category
Operator symbol
Operator name
Example
User overloadable
Primary
.
Member access
x.y
No
Primary
-> (unsafe)
Pointer to struct
x->y
No
Primary
()
Function call
x()
No
Primary
[]
Array/index
a[x]
Via indexer
Primary
++
Post-increment
x++
Yes
Primary
--
Post-decrement
x--
Yes
Primary
new
Create instance
new Test()
No
Primary

stackalloc

Unsafe stack
allocation
stackalloc(10)

No

Primary

typeof

Get type from
identifier
typeof(int)

No

Primary

nameof

Get name of
identifier
nameof(x)

No

Primary

checked

Integral overflow
check on
checked(x)

No

Primary

unchecked

Integral overflow
check off
unchecked(x)

No

Primary
default
Default value
default(char)
No
Unary
await
Await
await myTask
No
Unary
sizeof
Get size of struct
sizeof(int)
No
Unary
?.
Null-conditional
x?.y
No
Unary
+
Positive value of
+x
Yes
Unary
-
Negative value of
-x
Yes
Unary
!
Not
!x
Yes
Unary
~
Bitwise complement
~x
Yes
Unary
++
Pre-increment
++x
Yes
Unary
--
Pre-decrement
--x
Yes
Unary
()
Cast
(int)x
No
Unary
* (unsafe)
Value at address
*x
No
Unary
& (unsafe)
Address of value
&x
No
Multiplicative
*
Multiply
x * y
Yes
Multiplicative
/
Divide
x / y
Yes
Multiplicative
%
Remainder
x % y
Yes
Additive
+
Add
x + y
Yes
Additive
-
Subtract
x - y
Yes
Shift
<<
Shift left
x << 1
Yes
Shift
>>
Shift right
x >> 1
Yes
Relational
<
Less than
x < y
Yes
Relational
>
Greater than
x > y
Yes
Relational
<=
Less than or equal to
x <= y
Yes
Relational

>=

Greater than or
equal to
x >= y

Yes

Relational

is

Type is or is subclass
of
x is y

No

Relational
as
Type conversion
x as y
No
Equality
==
Equals
x == y
Yes
Equality
!=
Not equals
x != y
Yes
Logical And
&
And
x & y
Yes
Logical Xor
^
Exclusive Or
x ^ y
Yes
Logical Or
|
Or
x | y
Yes
Conditional And
&&
Conditional And
x && y
Via &
Conditional Or
||
Conditional Or
x || y
Via |
Null coalescing
??
Null coalescing
x ?? y
No
Conditional


?:


Conditional


isTrue ? thenThis
Value : elseThis
Value
No


Assignment &
Lambda
=

Assign

x = y

No

Assignment
*=
Multiply self by
x *= 2
Via *
Assignment
/=
Divide self by
x /= 2
Via /
Assignment
+=
Add to self
x += 2
Via +
Assignment
-=
Subtract from self
x -= 2
Via -
Assignment
<<=
Shift self left by
x <<= 2
Via <<
Assignment
>>=
Shift self right by
x >>= 2
Via >>
Assignment
&=
And self by
x &= 2
Via &
Assignment
^=
Exclusive-Or self by
x ^= 2
Via ^
Assignment
|=
Or self by
x |= 2
Via |
Assignment
=>
Lambda
x => x + 1
No