Thursday, 19 February 2015

C# list of Operators and precedence table

This post demonstrates available C# operators and their order of precedence.
C# provides a large set of operators, which are symbols that specify which type of operations to perform in an expression.
A language may contain a fixed number of built-in operators (e.g. +, -, *, < etc. in C and C++), or it may allow the creation of user-defined operators (e.g. F#, OCaml, Haskell).

In C#, we are allowed to create user-defined types for operators by using operator overloading, thus changing their meaning when applied. To know how to define your own operators – Operator Overloading
The table lists the C# operators grouped in order of precedence from highest to lowest.
Operators within each group have equal precedence. For operators with equal precedence (e.g., + and -) are evaluated in an expression based on the associativity. Please follow this link to know the associativity of operators with equal precedence - Associativity of C# Operators

 
Operator Category Operators
Primary x.y
f(x)
a[x]
x++
x--
new
typeof
checked
unchecked
default(T)
delegate
sizeof
->
Unary +x
-x
!x
~x
++x
--x
(T)x
await
&x
*x
Multiplicative x*y
x/y
x%y
Additive x+y
x-y
Shift x<
x>>y
Relational and type testing x
x>y
x<=y
x>=y
is
as
Equality x==y
x!=y
Logical AND x & y
Logical XOR x ^ y
Logical OR x | y
Conditional AND x && y
Conditional OR x || y
Null-coalescing x ?? y
Conditional ?:
Assignment and lambda expression x=y
x+=y
x-=y
x*=y
x/=y
x%=y
x&=y
x|=y
x^=y
x<<=y
x>>=y
=>

Note - The arithmetic operators e.g. addition, subtraction can produce results outside the range of possible values causes an exception depending on the execution context, which can be checked or unchecked. To know more about checked and unchecked - Click here

Please share your thoughts on this post in the comments section.

Karthik Byggari

Author & Editor

Computer Science graduate, Techie, Founder of logicallyproven, Love to Share and Read About pprogramming related things.

3 comments:

 
biz.