Types of Operators in C#
Stop Getting Confused!
In C#, operators are special symbols used to perform operations on variables and values, such as addition, comparison, logical operations, and assignment.
Example:
int a = 10;
int b = 20;
int result = a + b;
Console.WriteLine(result); // 30
Here, + is an arithmetic operator.
1. Types of Operators in C#
| # | Operator Type | Operators | |---|---|---| | 1 | Arithmetic | +, -, *, /, % | | 2 | Relational / Comparison | ==, !=, >, <, >=, <= | | 3 | Logical | &&, ||, ! | | 4 | Assignment | =, +=, -=, *=, /=, %= | | 5 | Increment / Decrement | ++, -- | | 6 | Bitwise | &, \|, ^, ~ | | 7 | Shift | <<, >>, >>> | | 8 | Conditional / Ternary | ?: | | 9 | Null-Coalescing | ??, ??= | | 10 | Null-Conditional | ?., ?[] | | 11 | Type Testing / Conversion | is, as, typeof, sizeof | | 12 | Member / Element Access | ., [], () | | 13 | Object Creation | new | | 14 | Other Special Operators | nameof, await, =>, checked, unchecked |
2. Arithmetic Operators
Used to perform mathematical calculations.
int a = 10, b = 3;
Console.WriteLine(a + b); // 13
Console.WriteLine(a - b); // 7
Console.WriteLine(a * b); // 30
Console.WriteLine(a / b); // 3
Console.WriteLine(a % b); // 1
Note: Integer division (10 / 3) returns 3, not 3.33.
3. Relational / Comparison Operators
Used to compare two values. The result is true or false.
int a = 10, b = 20;
Console.WriteLine(a == b); // False
Console.WriteLine(a != b); // True
Console.WriteLine(a > b); // False
Console.WriteLine(a < b); // True
Console.WriteLine(a >= b); // False
Console.WriteLine(a <= b); // True
4. Logical Operators
Used to combine or reverse Boolean conditions.
int age = 25;
bool hasLicense = true;
Console.WriteLine(age >= 18 && hasLicense); // True
Console.WriteLine(age < 18 || hasLicense); // True
Console.WriteLine(!hasLicense); // False
-
&&— AND: Both conditions must be true. -
||— OR: At least one condition must be true. -
!— NOT: Reverses the Boolean value.
5. Assignment Operators
Used to assign or update values.
int a = 10;
a += 5; // 15
a -= 3; // 12
a *= 2; // 24
a /= 4; // 6
a %= 4; // 2
For example, a += 5 is equivalent to a = a + 5 for this integer example.
6. Increment and Decrement Operators
Used to increase or decrease a value by one.
int a = 10;
Console.WriteLine(++a); // 11 (Pre-increment)
Console.WriteLine(a++); // 11 (Post-increment)
Console.WriteLine(a); // 12
Console.WriteLine(--a); // 11 (Pre-decrement)
Console.WriteLine(a--); // 11 (Post-decrement)
Key difference:
-
++a— Increments first, then returns the value. -
a++— Returns the original value, then increments.
7. Bitwise Operators
Used to perform operations on individual bits.
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
Console.WriteLine(a & b); // 1
Console.WriteLine(a | b); // 7
Console.WriteLine(a ^ b); // 6
Console.WriteLine(~a); // -6
8. Shift Operators
Used to shift binary bits left or right.
int a = 8;
Console.WriteLine(a << 1); // 16
Console.WriteLine(a >> 1); // 4
Console.WriteLine(a >>> 1); // 4
>>> is the unsigned right-shift operator, available since C# 11.
9. Conditional / Ternary Operator
A shorthand alternative to a simple if-else.
Syntax:
condition ? valueIfTrue : valueIfFalse;
Example:
int age = 20;
string result = age >= 18 ? "Adult" : "Minor";
Console.WriteLine(result); // Adult
10. Null-Coalescing Operators
Used to provide default values when a variable is null.
string? name = null;
string result = name ?? "Guest";
Console.WriteLine(result); // Guest
Null-coalescing assignment (??=):
string? name = null;
name ??= "Benazir";
Console.WriteLine(name); // Benazir
11. Null-Conditional Operators
Used to safely access members when an object might be null.
string? name = null;
int? length = name?.Length;
Console.WriteLine(length); // Prints a blank line
Without ?., accessing name.Length when name is null would throw a NullReferenceException.
12. Type Testing / Conversion Operators
Used to check or convert types.
object value = "Hello";
Console.WriteLine(value is string); // True
string? text = value as string;
Console.WriteLine(text); // Hello
Console.WriteLine(typeof(int)); // System.Int32
13. Member and Element Access Operators
Used to access object members, array elements, and invoke methods.
string name = "Benazir";
Console.WriteLine(name.Length); // 7
int[] numbers = { 10, 20, 30 };
Console.WriteLine(numbers[0]); // 10
Here, ., [], and () are used for member access, indexing, and invocation.
14. Object Creation Operator
Used to create objects.
List<string> names = new List<string>();
names.Add("Benazir");
names.Add("John");
15. Other Special Operators
// nameof
Console.WriteLine(nameof(Console)); // Console
// Lambda operator
Func<int, int> square = x => x * x;
// checked
int a = checked(int.MaxValue + 0);
await is used with asynchronous operations, while checked and unchecked control overflow checking for certain numeric operations.
Important Operators for Interviews
|
Priority |
Operator Type |
|---|---|
|
High |
Arithmetic |
|
High |
Relational / Comparison |
|
High |
Logical |
|
High |
Assignment |
|
High |
Increment / Decrement |
|
High |
Ternary |
|
High |
Null-Coalescing |
|
High |
Null-Conditional |
|
Medium |
Type Testing / Conversion |
|
Medium |
Bitwise and Shift |
|
Medium |
Member Access and Object Creation |
Important interview questions: What is the difference between == and Equals()? Between && and &? Between ?? and ?.? Between ++a and a++? How does operator precedence work in C#?
These are the key operator concepts to understand for C# fundamentals and technical interviews.