Operators in C#
Operators in C# allow you to perform various operations on variables and values, such as arithmetic, comparison, logical, and more. This guide introduces the different types of operators available in C# and provides examples to help you understand how to use them.
Video Explanation

1. Types of Operators in C#
C# supports various operators, which can be broadly categorized as follows:
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Assignment Operators
- Unary Operators
- Ternary Operator
2. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations.
| Operator | Description | Example |
|---|---|---|
+ | Addition | x + y |
- | Subtraction | x - y |
* | Multiplication | x * y |
/ | Division | x / y |
% | Modulus (Remainder) | x % y |
Example:
int a = 10, b = 5;
Console.WriteLine(a + b); // Output: 15
Console.WriteLine(a - b); // Output: 5
Console.WriteLine(a * b); // Output: 50
Console.WriteLine(a / b); // Output: 2
Console.WriteLine(a % b); // Output: 0
3. Comparison Operators
Comparison operators are used to compare two values.
| Operator | Description | Example |
|---|---|---|
== | Equal to | x == y |
!= | Not equal to | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal | x >= y |
<= | Less than or equal | x <= y |
Example:
int x = 10, y = 20;
Console.WriteLine(x == y); // Output: False
Console.WriteLine(x != y); // Output: True
Console.WriteLine(x > y); // Output: False
Console.WriteLine(x < y); // Output: True
4. Logical Operators
Logical operators are used to combine multiple conditions.
| Operator | Description | Example |
|---|---|---|
&& | Logical AND | x && y |
| ` | ` | |
! | Logical NOT | !x |
Example:
bool isAdult = true, hasID = false;
Console.WriteLine(isAdult && hasID); // Output: False
Console.WriteLine(isAdult || hasID); // Output: True
Console.WriteLine(!isAdult); // Output: False