C# Operators — including arithmetic, logical, comparison, assignment, bitwise, and more — with examples and descriptions:
1. Arithmetic Operators
| Operator | Name | Example | Description |
|---|---|---|---|
+ |
Addition | a + b |
Adds two operands |
- |
Subtraction | a - b |
Subtracts second operand from first |
* |
Multiplication | a * b |
Multiplies operands |
/ |
Division | a / b |
Divides numerator by denominator |
% |
Modulus | a % b |
Returns remainder of division |
++ |
Increment | a++ or ++a |
Increases value by 1 |
-- |
Decrement | a-- or --a |
Decreases value by 1 |
2. Assignment Operators
| Operator | Example | Equivalent To | Description |
|---|---|---|---|
= |
a = 5 |
— | Assigns value |
+= |
a += 2 |
a = a + 2 |
Adds and assigns |
-= |
a -= 2 |
a = a - 2 |
Subtracts and assigns |
*= |
a *= 3 |
a = a * 3 |
Multiplies and assigns |
/= |
a /= 2 |
a = a / 2 |
Divides and assigns |
%= |
a %= 2 |
a = a % 2 |
Modulus and assigns |
3. Comparison / Relational Operators
| Operator | Example | Description |
|---|---|---|
== |
a == b |
Returns true if values are equal |
!= |
a != b |
Returns true if not equal |
> |
a > b |
Greater than |
< |
a < b |
Less than |
>= |
a >= b |
Greater than or equal to |
<= |
a <= b |
Less than or equal to |
4. Logical Operators
| Operator | Name | Example | Description |
|---|---|---|---|
&& |
AND | a && b |
True if both are true |
| ` | ` | OR | |
! |
NOT | !a |
Inverts the Boolean value |
5. Bitwise Operators
| Operator | Name | Example | Description |
|---|---|---|---|
& |
AND | a & b |
Performs bitwise AND |
| ` | ` | OR | `a |
^ |
XOR | a ^ b |
Performs bitwise exclusive OR |
~ |
NOT | ~a |
Inverts all bits |
<< |
Left Shift | a << 2 |
Shifts bits to the left |
>> |
Right Shift | a >> 2 |
Shifts bits to |
Read More
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/
Leave Comment