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.

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

 


 2. Assignment Operators

Used to assign values.

OperatorExampleMeaning
=x = 5Assign
+=x += 3x = x + 3
-=x -= 2x = x - 2
*=x *= 4x = x * 4
/=x /= 2x = x / 2
%=x %= 2x = x % 2

 3. Comparison Operators

Used to compare values (returns true or false).

OperatorDescription
==Equal to
!=Not equal
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal


 4. Logical Operators

Used to combine conditions.

OperatorDescription
&&Logical AND
`
!Logical NOT


 5. Unary Operators

Operate on a single operand.

OperatorExample
++x++
--x--
!!isActive


 6. Bitwise Operators (Advanced)

OperatorDescription
&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...