📌1. Arithmetic Operators
Work on numbers.
int a = 10, b = 3;
Console.WriteLine(a + b); // 13 (Addition) C
onsole.WriteLine(a - b); // 7 (Subtraction)
Console.WriteLine(a * b); // 30 (Multiplication)
Console.WriteLine(a / b); // 3 (Division, integer division)
Console.WriteLine(a % b); // 1 (Modulus, remainder)
📌2. Unary Operators
Work on a single operand.
int x = 5; Console.WriteLine(+x); // 5
Console.WriteLine(-x); // -5
Console.WriteLine(++x); // 6 (pre-increment)
Console.WriteLine(x++); // 6 (post-increment, then x=7)
Console.WriteLine(--x); // 6 (pre-decrement)
Console.WriteLine(x--); // 6 (post-decrement, then x=5)
📌3. Assignment Operators
Assign values.
int n = 10;
n += 5; // 15
n -= 2; // 13
n *= 3; // 39
n /= 3; // 13
n %= 5; // 3
📌4. Comparison (Relational) Operators
Return true
or false
.
int a = 10, b = 20; C
onsole.WriteLine(a == b); // false
Console.WriteLine(a != b); // true
Console.WriteLine(a > b); // false
Console.WriteLine(a < b); // true
Console.WriteLine(a >= 10); // true
Console.WriteLine(b <= 15); // false
📌5. Logical Operators
Work on bool
values.
bool t = true, f = false;
Console.WriteLine(t && f); // false (AND)
Console.WriteLine(t || f); // true (OR)
Console.WriteLine(!t); // false (NOT)
📌6. Bitwise Operators
Work on bits of integers.
int a = 6; // 110 in binary
int b = 3; // 011 in binary
Console.WriteLine(a & b); // 2 (010) AND
Console.WriteLine(a | b); // 7 (111) OR
Console.WriteLine(a ^ b); // 5 (101) XOR
Console.WriteLine(~a); // -7 (bitwise NOT, two’s complement)
Console.WriteLine(a << 1);// 12 (shift left)
Console.WriteLine(a >> 1);// 3 (shift right)
📌7. Conditional / Ternary Operator
Short if-else.
int age = 18;
string status = (age >= 18) ? "Adult" : "Minor";
Console.WriteLine(status); // Adult
📌8. Null-Coalescing Operators
Used with nullable values.
string name = null;
string user = name ?? "Guest"; // if null, use "Guest"
int? number = null;
int value = number ?? 100; // if null, use 100
📌9. Null-Conditional Operator
Avoids NullReferenceException
.
string text = null;
int? length = text?.Length; // null instead of exception
📌10. Type Operators
object obj = "Hello";
// is → type check
Console.WriteLine(obj is string); // true
// as → safe cast (returns null if cast fails)
string s = obj as string;
// typeof → get Type info
Console.WriteLine(typeof(int)); // System.Int32
// sizeof → get size in bytes (only in unsafe code)
Console.WriteLine(sizeof(int)); // 4
📌11. Other Operators
-
checked
/unchecked
→ control overflow checking. See Arithmetic Overflow -
=>
→ lambda expressions. -
::
→ namespace alias qualifier. -
new
→ create objects. -
[]
→ array or indexer access. -
.
→ member access. -
()
→ method call or grouping.