C# Operators

Operators are special symbols or keywords that perform operations on one or more operands. C# provides a rich set of operators that can be broadly categorized as follows:

Arithmetic Operators

These operators are used for performing mathematical calculations.

Operator Description Example
+ Addition int sum = a + b;
- Subtraction int difference = a - b;
* Multiplication int product = a * b;
/ Division int quotient = a / b;
% Modulo (Remainder) int remainder = a % b;
++ Increment (adds 1) x++; or ++x;
-- Decrement (subtracts 1) x--; or --x;

Relational Operators

These operators are used to compare two values and return a boolean result (true or false).

Operator Description Example
== Equal to if (a == b)
!= Not equal to if (a != b)
> Greater than if (a > b)
< Less than if (a < b)
>= Greater than or equal to if (a >= b)
<= Less than or equal to if (a <= b)

Logical Operators

These operators are used to combine or modify boolean expressions.

Operator Description Example
&& Logical AND (short-circuiting) if (condition1 && condition2)
|| Logical OR (short-circuiting) if (condition1 || condition2)
! Logical NOT if (!condition)
& Bitwise AND (also logical AND if operands are bool) int result = x & y;
| Bitwise OR (also logical OR if operands are bool) int result = x | y;
^ Bitwise XOR int result = x ^ y;
?? Null-coalescing operator string name = nullableName ?? "Default";

Assignment Operators

These operators are used to assign values to variables. They can be combined with arithmetic operators for shorthand.

Operator Description Equivalent
= Assignment x = 5;
+= Add and assign x += 2; is equivalent to x = x + 2;
-= Subtract and assign x -= 2; is equivalent to x = x - 2;
*= Multiply and assign x *= 2; is equivalent to x = x * 2;
/= Divide and assign x /= 2; is equivalent to x = x / 2;
%= Modulo and assign x %= 2; is equivalent to x = x % 2;
&= Bitwise AND and assign x &= y; is equivalent to x = x & y;
|= Bitwise OR and assign x |= y; is equivalent to x = x | y;
^= Bitwise XOR and assign x ^= y; is equivalent to x = x ^ y;
<<= Left shift and assign x <<= 2; is equivalent to x = x << 2;
>>= Right shift and assign x >>= 2; is equivalent to x = x >> 2;

Bitwise and Shift Operators

These operators work on the binary representation of integral types.

Operator Description Example
& Bitwise AND byte result = x & y;
| Bitwise OR byte result = x | y;
^ Bitwise XOR byte result = x ^ y;
~ Bitwise Complement (NOT) int inverted = ~x;
<< Left Shift int shifted = x << 2;
>> Right Shift int shifted = x >> 2;

Other Operators

C# includes several other useful operators:

Operator Precedence and Associativity

Operators have a precedence level that determines the order in which they are evaluated. For operators with the same precedence, associativity (left-to-right or right-to-left) determines the order. Parentheses can be used to override default precedence.

Understanding operators is fundamental to writing effective C# code. For a comprehensive list and detailed explanations, refer to the official C# Operator Reference.