C# Operators
C# Operators
Operators in C# are symbols used to perform operations on variables and values, such as arithmetic calculations, comparisons, and logical decisions.
Types of Operators in C#
Arithmetic Operators
Assignment Operators
Comparison (Relational) Operators
Logical Operators
Unary Operators
Bitwise Operators
Ternary Operator
1. Arithmetic Operators
Used for mathematical calculations.
| Operator | Description | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (remainder) | a % b |
2. Assignment Operators
Used to assign values.
| Operator | Example | Meaning |
|---|---|---|
= | x = 5 | Assign |
+= | x += 3 | x = x + 3 |
-= | x -= 2 | x = x - 2 |
*= | x *= 4 | x = x * 4 |
/= | x /= 2 | x = x / 2 |
%= | x %= 2 | x = x % 2 |
3. Comparison Operators
Used to compare values (returns true or false).
| Operator | Description |
|---|---|
== | Equal to |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
4. Logical Operators
Used to combine conditions.
| Operator | Description |
|---|---|
&& | Logical AND |
| ` | |
! | Logical NOT |
5. Unary Operators
Operate on a single operand.
| Operator | Example |
|---|---|
++ | x++ |
-- | x-- |
! | !isActive |
6. Bitwise Operators (Advanced)
| Operator | Description |
|---|---|
& | AND |
| ` | ` |
^ | XOR |
~ | NOT |
<< | Left shift |
>> | Right shift |
7. Ternary Operator
Short form of if-else.
Operator Precedence
Operators are evaluated in a specific order:
()* / %+ -Comparison
Logical
Assignment
Summary
✔ Operators perform operations
✔ Arithmetic for math
✔ Comparison returns boolean
✔ Logical combines conditions
✔ Ternary simplifies if-else
