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#

  1. Arithmetic Operators

  2. Assignment Operators

  3. Comparison (Relational) Operators

  4. Logical Operators

  5. Unary Operators

  6. Bitwise Operators

  7. 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:

  1. ()

  2. * / %

  3. + -

  4. Comparison

  5. Logical

  6. Assignment


 Summary

✔ Operators perform operations
✔ Arithmetic for math
✔ Comparison returns boolean
✔ Logical combines conditions
✔ Ternary simplifies if-else

You may also like...