Operators

In every programming language an operator plays a major role for some operations or manipulations. C# also provides a wide range of operators which tells the computer to do some specific mathematical operations or perform a logical manipulations.

Below is the list of types of the operators C# supports

1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators

We will see one by one type of operators in details and it's usage

1. Arithmetic Operators

Below table shows all the arithmetic operators which C# supports. We will see some examples of those by assuming that X = 5 and Y = 10.

X - Y = -5
OperatorDescriptionExample
+ (Addition)Adds two operands X + Y = 15
- (Minus)Substract second operand from first
* (Multiplication)Multiplies two operands X * Y = 50
/ (Division)Divides one operand by another Y / X = 5
% (Modulus)Returns the remainder of divide operationY % X = 0
++ (Incremental)Increases value by 1X++ = 6
-- (Decremental)Decreases value by 1X-- = 4

2. Relational Operators

We can also say comparision operators, which will compare the two values and return the boolean value. Below table shows all the relational operators which C# supports. We will see some examples of those by assuming that X = 5 and Y = 10.

OperatorDescriptionExample
== equal to X == Y return false
!= not equal to X != Y return true
> greater than X > Y return false
< less than X < Y return true
>= greater than or equal to X >= Y return false
<= less than or equal to X <= Y return true

3. Logical Operators

Logical operators are used to join mutliple expressions into one condition. Below table shows all the logical operators which C# supports. We will see some examples of those by assuming that X = 5 and Y = 10.

OperatorDescriptionExample
&& Logical AND ((X < 10) && (Y < 10)) = false
|| Logical OR ((X < 10) || (Y < 10)) = true
! Logical NOT !(X < 10) = false

4. Assignment Operators

When we want to assign some values to variable then assignment operators are used. Below table shows all the assignment operators which C# supports. We will see some example of that by assuming that X = 5 and Y = 10.

OperatorDescriptionExample
= assignment X = 5, X will be 5
+= add and assignment X += 3 = 8
-= substract and assignment X -= 3 = 2
*= multiply and assignment X *= 3 = 15
/= divide and assignment X /= 3 = 1
%= modulus and assignment X %= 3 = 2
&= BITWISE and assignment X &= 3 = 1
|= BITWISE inclusive OR and assignment X |= 3 = 7
^= bitwise exclusive OR and assignment operator X ^= 3 = 6
>>= Right shift AND assignment operator X >>= 3 = 4
<<= Left shift AND assignment operator X <<= 3 = 40

 

← Previous

Next →