C Sharp Operators

C# operators

The symbols used to perform operations like arithmetic, logical, bitwise, etc. are known as operators. Different types of operations can be performed in C# language using the different types of operators that are listed below:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Unary Operators
  • Ternary Operators
  • Misc Operators

Precedence and Associativity of Operators in C#:

To determine the operator that will be evaluated first and next, the precedence of the operator is taken into consideration. To determine the direction of the operators to be evaluated, which can be left to right or right to left, the associativity of the operator is taken into consideration.

Example:

int x = 2+ 5*2

Explanation:

The value of x will be 12 because the multiplicative operator (*) is evaluated before the additive operator (+).

The precedence and associativity of the C# operators are listed below:

Category (By Precedence) Operator(s) Associativity
Unary + – ! ~ ++ — (type)* & sizeof Right to Left
Additive + – Left to Right
Multiplicative Left to Right
Relational < > <= >= Left to Right
Shift << >> Left to Right
Equality == != Right to Left
Logical AND & Left to Right
Logical OR | Left to Right
Logical XOR ^ Left to Right
Conditional OR || Left to Right
Conditional AND && Left to Right
Null Coalescing ?? Left to Right
Ternary ?: Right to Left
Assignment = *= /= Right to Left