Arithmetic Operators

The arithmetic operators perform arithmetic operations on all the numeric type operands such as sbyte, byte, short, ushort, int, uint, long, ulong, float, double, and decimal.

OperatorNameDescriptionExample
+AdditionComputes the sum of left and right operands.int x = 5 + 5;
SubtractionSubtract the right operand from the left operandint x = 5 – 1;
*MultiplicationMultiply left and right operandint x = 5 * 1;
/DivisionDivides the left operand by the right operandint x = 10 / 2;
%ReminderComputes the remainder after dividing its left operand by its right operandint x = 5 % 2;
++Unary incrementUnary increment ++ operator increases its operand by 1x++
Unary decrementUnary decrement — operator decreases its operand by 1x–
+Unary plusReturns the value of operand+5
Unary minusComputes the numeric negation of its operand.-5

Assignment Operators

The assignment operator = assigns its right had value to its left-hand variable, property, or indexer. It can also be used with other arithmetic, Boolean logical, and bitwise operators.

OperatorNameDescriptionExample
=AssignmentAssigns its right had value to its left-hand variable, property or indexer.x = 10;
x op= yCompound assignmentShort form of x =x op y where op = any arithmetic, Boolean logical, and bitwise operator.x += 5;
??=Null-coalescing assignmentC# 8 onwards, ??= assigns value of the right operand only if the left operand is nullx ??= 5;

Comparison Operators

Comparison operators compre two numeric operands and returns true or false.

OperatorDescriptionExample
<Returns true if the right operand is less than the left operandx < y;
>Returns true if the right operand is greater than the left operandx > y;
<=Returns true if the right operand is less than or equal to the left operandx <= y
>=Returns true if the right operand is greater than or equal to the left operandx >= y;

Equality Operators

The equality operator checks whether the two operands are equal or not.

OperatorDescriptionExample
==Returns true if operands are equal otherwise false.x == y;
!=Returns true if operands are not equal otherwise false.x != y;

Boolean Logical Operators

The Boolean logical operators perform a logical operation on bool operands.

OperatorDescriptionExample
!Reverses the bool result of bool expression. Returns false if result is true and returns true if result is false.!false
&Computes the logical AND of its operands. Returns true only when both true, otherwise false.x & y;
|Computes the logical OR of its operands. Returns true when any one result is true.x | y;
^Computes the logical exclusive of its operands. It returns true only if any one operand is true and another operand is false, otherwise returns false.x ^ y;
&&Computes the logical AND of its bool operands. Returns true both operands are true, otherwise returns false.x && y;
||Computes the logical OR of its bool operands. Returns true when any one operand is true.x || y;

Operator Evaluation & Precedence

Evaluation of the operands in an expression starts from left to right. If multiple operators are used in an expression, then the operators with higher priority are evaluated before the operators with lower priority.

The following table lists operators starting with the higher precedence operators to lower precedence operators.

OperatorsCategory
x.y, x?.y, x?[y], f(x), a[i], x++, x–, new, typeof, checked, unchecked, default, nameof, delegate, sizeof, stackalloc, x->yPrimary
+x, -x, !x, ~x, ++x, –x, ^x, (T)x, await, &x, *x, true and falseUnary
x..yRange
x * y, x / y, x % yMultiplicative
x + y, x – yAdditive
x << y, x >> yShift
x < y, x > y, x <= y, x >= y, is, asRelational and type-testing
x == y, x != yEquality
x & yBoolean logical AND
x ^ yBoolean logical XOR
x | yBoolean logical OR
x && yConditional AND
x || yConditional OR
x ?? yNull-coalescing operator
c ? t : fConditional operator
x = y, x += y, x -= y, x *= y, x /= y, x %= y, x &= y, x |= y, x ^= y, x <<= y, x >>= y, x ??= y, =>Assignment and lambda declaration

T