Operators are fundamental building blocks in C#. They allow you to perform operations on variables and values. Whether you are assigning values, comparing data, performing arithmetic, evaluating conditions, or manipulating bits, operators are everywhere in C# programming.

Understanding operators in depth is essential because:

This article provides a complete, descriptive, and practical guide to all operators in C#, covering everything from basic arithmetic to advanced operator overloading.

What Are Operators in C#?

Operators are special symbols that tell the compiler what kind of operation to perform on operands (variables or values).

Example

int result = 5 + 3;

Here:

C# provides a rich set of operators grouped into categories based on their purpose.

1. Arithmetic Operators

Arithmetic operators perform basic mathematical operations on numeric data.

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (remainder)a % b

Example

int a = 10;
int b = 3;

Console.WriteLine(a + b); // 13
Console.WriteLine(a % b); // 1

2. Assignment Operators

Assignment operators assign values to variables.

OperatorDescriptionExample
=Assign valuea = 10
+=Add and assigna += 5
-=Subtract and assigna -= 5
*=Multiply and assigna *= 2
/=Divide and assigna /= 2
%=Modulus and assigna %= 2

Example

int x = 10;
x += 5; // x = x + 5

3. Comparison (Relational) Operators

Used to compare two values. They return true or false.

OperatorMeaningExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equala >= b
<=Less than or equala <= b

Example

int a = 5, b = 10;
Console.WriteLine(a < b); // true

4. Logical Operators

Used for combining Boolean expressions.

OperatorMeaningExample
&&Logical ANDcond1 && cond2
``
!Logical NOT!cond

Example

bool isLogin = true;
bool isAdmin = false;

Console.WriteLine(isLogin && isAdmin); // false

5. Unary Operators

Unary operators require only one operand.

OperatorMeaningExample
+Unary plus+a
-Unary minus-a
++Incrementa++ or ++a
--Decrementa-- or --a
!Logical NOT!flag

Example

int a = 5;
Console.WriteLine(++a); // 6

6. Bitwise Operators

Used to manipulate bits at the low level.

OperatorMeaningExample
&Bitwise ANDa & b
``Bitwise OR
^Bitwise XORa ^ b
~Bitwise NOT~a
<<Left shifta << 1
>>Right shifta >> 1

Example

int a = 5;  // 0101
int b = 3;  // 0011
int c = a & b; // 0001 -> 1

7. Conditional (Ternary) Operator

Short form of if-else.

condition ? value_if_true : value_if_false;

Example

int age = 20;
string message = age >= 18 ? "Adult" : "Minor";

8. Null-Coalescing Operators

Used when working with nullable values.

1. Null Coalescing: ??

Returns a fallback value if the left side is null.

string name = inputName ?? "Unknown";

2. Null Coalescing Assignment: ??=

Assigns a value only if the variable is null.

name ??= "Guest";

9. Null-Conditional Operator ?.

Safely access a member without throwing null exceptions.

string name = person?.FullName;

10. Type Check Operators

1. is Operator

Checks whether an object is of a certain type.

if (obj is string)

2. as Operator

Converts type safely; returns null if conversion fails.

string data = obj as string;

11. Type Conversion Operators

typeof

Returns the Type object.

Type t = typeof(string);

sizeof

Returns the size of value types.

int size = sizeof(int); // 4

12. Member Access Operator .

Used to access fields, methods, and properties.

person.Name

13. Indexer Operator []

Used for accessing elements in arrays and collections.

int value = arr[0];

14. Lambda Operator =>

Used in lambda expressions.

(x, y) => x + y

15. Range Operator .. (C# 8+)

Used for slicing arrays.

var slice = arr[1..4];

16. Pattern Matching Operators (Modern C#)

is Expression Patterns

if (item is int number)

switch Expression Patterns

var result = input switch
{
    > 0 => "Positive",
    < 0 => "Negative",
    _ => "Zero"
};

17. Operator Overloading

C# allows custom operators in your classes.

Example

public static Point operator +(Point a, Point b)
{
    return new Point(a.X + b.X, a.Y + b.Y);
}

Operator overloading is powerful in mathematical and domain-driven models.

18. Precedence and Associativity

C# follows strict rules for evaluating expressions.

int result = 10 + 20 * 2; // Multiplication happens first

Always remember