Operators let you work with numbers, compare values, and combine conditions. C# provides a rich set of operators to perform different operations on variables and values. Operators are grouped by their functionality and usage. Here’s a complete reference:
Arithmetic Operators
Operator |
Name |
Example |
Result |
+ |
Addition |
3 + 2 |
5 |
- |
Subtraction |
5 - 2 |
3 |
* |
Multiplication |
4 * 2 |
8 |
/ |
Division |
8 / 2 |
4 |
% |
Modulus |
7 % 3 |
1 |
++ |
Increment |
a++ |
a = a + 1 |
-- |
Decrement |
a-- |
a = a - 1 |
Assignment Operators
Operator |
Example |
Equivalent |
= |
a = 5 |
|
+= |
a += 2 |
a = a + 2 |
-= |
a -= 2 |
a = a - 2 |
*= |
a *= 2 |
a = a * 2 |
/= |
a /= 2 |
a = a / 2 |
%= |
a %= 2 |
a = a % 2 |
&= |
a &= b |
a = a & b |
|= |
a |= b |
a = a | b |
^= |
a ^= b |
a = a ^ b |
<<= |
a <<= 2 |
a = a << 2 |
>>= |
a >>= 2 |
a = a >> 2 |
Comparison (Relational) Operators
Operator |
Name |
Example |
Result |
== |
Equal to |
a == b |
true/false |
!= |
Not equal to |
a != b |
true/false |
> |
Greater than |
a > b |
true/false |
< |
Less than |
a < b |
true/false |
>= |
Greater or equal |
a >= b |
true/false |
<= |
Less or equal |
a <= b |
true/false |
Logical Operators
Operator |
Name |
Example |
Result |
&& |
AND |
a && b |
true if both are true |
|| |
OR |
a || b |
true if either is true |
! |
NOT |
!a |
true if a is false |
Bitwise Operators
Operator |
Name |
Example |
Result |
& |
AND |
a & b |
Bitwise AND |
| |
OR |
a | b |
Bitwise OR |
^ |
XOR |
a ^ b |
Bitwise XOR |
~ |
NOT |
~a |
Bitwise NOT |
<< |
Left shift |
a << 2 |
Shifts bits left |
>> |
Right shift |
a >> 2 |
Shifts bits right |
Other Operators
Operator |
Name |
Example |
Description |
? : |
Ternary/Conditional |
a ? b : c |
If a is true, returns b , else c |
?? |
Null-coalescing |
a ?? b |
Returns a if not null, else b |
?. |
Null-conditional |
a?.b |
Returns b if a not null, else null |
=> |
Lambda |
x => x*x |
Used for lambda expressions |
is |
Type check |
a is int |
Checks if a is of type int |
as |
Safe cast |
a as string |
Casts a to string, or null |
sizeof |
Size in bytes |
sizeof(int) |
Gets size of type (unsafe context) |
typeof |
Type info |
typeof(int) |
Gets the System.Type object |
checked |
Overflow check |
checked(a + b) |
Checks for overflow |
unchecked |
Ignore overflow |
unchecked(a+b) |
Ignores overflow |
new |
Object creation |
new MyClass() |
Instantiates a new object |
delegate |
Delegate creation |
delegate {} |
Anonymous method/delegate |
Operator Precedence
Operators are evaluated in a specific order. Use parentheses to clarify complex expressions.
Examples
1
2
3
4
5
6
| int a = 10, b = 3;
Console.WriteLine(a + b); // 13
Console.WriteLine(a > b && b != 0); // true
Console.WriteLine(a << 1); // 20 (bitwise left shift)
string name = null;
Console.WriteLine(name ?? "Unknown"); // "Unknown"
|
💡 Try It!
Write a program that checks if someone is eligible to vote (age 18 or older):
1
2
3
4
5
6
7
| Console.Write("Enter your age: ");
int age = int.Parse(Console.ReadLine() ?? "0");
if (age >= 18) {
Console.WriteLine("You can vote!");
} else {
Console.WriteLine("Sorry, you are too young.");
}
|
Next: Learn about type conversion and manipulation in C#.