C# - Operators
Operators are used to perform operation on a single operand or two operands. Operators in C# can be categorized as follows:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- Bitwise operators
- Miscellaneous operators
C# Arithmetic operators
Arithmetic operators are used to perform arithmetic operations on two operands.
|+||Addition||Add two values|
|-||Subtraction||Subtract one value from another|
|*||Multiplication||Multiply two values|
|/||Division||Divide one value by another|
|%||Modulo||Returns remainder of division operation|
C# Assignment operators
Assignment operators are used to assign values of right hand side expression to left hand side operand.
|=||a = 5||a = 5||Example|
|+=||a += b||a = a + b|
|-=||a -= b||a = a - b|
|*=||a *= b||a = a * b|
|/=||a /= b||a = a / b|
|%=||a %= b||a = a % b|
|&=||a &= b||a = a & b||More Info|
||=||a |= b||a = a | b||More Info|
|^=||a ^= b||a = a ^ b||More Info|
|>>=||a >>= b||a = a >> b||More Info|
|<<=||a <<= b||a = a << b||More Info|
C# Comparison operators
Comparison operators are used to compare values of two operands. It returns true when values matches and returns false when values does not match.
|>=||Greater than or equal to|
|<=||Less than or equal to|
C# Increment/Decrement operators
Increment and decrement operators are used to increase and decrease the value of variable.
|++x||Pre-increment||Increases the value of x by 1, then returns x.||Example|
|x++||Post-increment||Returns x, then increases the value of x by 1.|
|--x||Pre-decrement||Decreases the value of x by 1, then returns x.||Example|
|x--||Post-decrement||Returns x, then decreases the value of x by 1.|
C# Logical operators
Logical operators are used to combine two or more conditions.
|&&||AND||Returns true when all conditions are true|
|||||OR||Returns true when any of the conditions is true|
|!||NOT||Returns opposite boolean result|
C# Bitwise operators
Bitwise operators are used to perform bitwise operations on two operands.
|&||AND||Returns 1 if both bits at the same position in both operands are 1, else returns 0||More Info|
||||OR||Returns 1 if one of two bits at the same position in both operands is 1, else returns 0||More Info|
|^||XOR||Returns 1 if only one of two bits at the same position in both operands is 1, else returns 0||More Info|
|~||NOT||Reverse all the bits||More Info|
|>>||Right shift||The left operand is moved right by the number of bits present in the right operand||More Info|
|<<||Left shift||The left operand value is moved left by the number of bits present in the right operand||More Info|
C# Miscellaneous operators
The table below describes other operators supported by C#:
|sizeof()||Returns size of a data type, constants or variable.|
|ternary operator (?:)||Returns one of the two values based on value of boolean expression.|
|Address-of operator (&)||Returns address of a variable.|
|Dereference operator (*)||Pointer to a variable.|
|Null coalescing operator (??)||Returns right-hand side operand when left-hand side operand is null, otherwise returns left-hand side operand.|
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.
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.
|18||x++ x--||Postfix increment, Postfix decrement|
|x.y||Member access expression|
|!||! (null-forgiving) operator|
|checked unchecked||checked and unchecked keyword|
|default||default value expressions|
|x->y||Pointer member access operator|
|17||++x --x||Prefix increment, Prefix decrement|
|+x -x||Unary plus, Unary minus|
|^x||Index from end operator|
|*x||Indirection (dereference) operator|
|true false||Boolean true and false operator|
|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|
|2||x?a:b||Conditional (ternary) operator|
|+= -= *= /= %=||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|
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.