Different types of operators are available in C#:

**Arithmetic**

Arithmetic operators are of two types:

**Binary****Unary**

**Binary (2 operands)**

Binary operators are those that require two operands to perform their action.

Different binary operators in C# are:

**+**used for addition.**–**used for subtraction.*****used for multiplication.**/**used for division. It returns quotient.- % used for modulus. It returns the remainder.

**+ Add**

This operator adds two values.

E.g.

The2+3result will be5.10+20result will be30. It can be used as10+20+30. In this case, it willadd 10 & 20then the result will be added to30, and the result will be60.

**– Subtract**

This operator is used for subtraction of two values. It will subtract the second operand from the first operand.

E.g.

5-3result will be2.10-20result will be-10. It can be used as10-20-30. In this case, it willsubtract 20 from 10and then30will be subtracted from the result. Final result will be-40.

*** Multiply**

This operator multiplies two values.

2*3will give us6.15*2will give us30.10*20*30will give us6000. It will firstmultiply 10 & 20and the result will be multiplied to30.

**/ Divide**

This operator is used to divide two numbers. It will divide the first operand by 2nd operand.

E.g.

10/5will give us2.5/10will give us0. This is because Divide operator only returns quotient.

**% Modulus**

This operator is used to get the remainder of two values when they are divided.

E.g.

10/ 5will give us0. While5/10will give us5. Because remainder will be5.

**Unary (Single operand)**

**++ increment****— decrement****– Minus**

**++ Increment**

This operator is used to increase the value of operand by 1. It is of two types:

**1- Pre Increment**

This will increase the value of a variable before it (Variable) is used. And is written as:

Letx = 10and its pre-increment will be++x.

**2- Post Increment**

This will increase the value of a variable after it (Variable) is used. And is written as:

Letx = 10and its post-increment will bex++.

**– – Decrement**

This operator is used to decrease the value of operand by 1. It is of two types:

**1- Pre Decrement**

This will decrease the value of a variable before it (Variable) is used. And is written as:

Letx = 10and its pre-decrement will be--x.

**2- Post Decrement**

This will decrease the value of a variable after it (Variable) is used. And is written as:

Letx = 10and its post-decrement will bex--.

**– Minus**

It is used to take negative of an operand.

E.g.

Letx = 10;then–xwill give us-10.

**Note: – This operator is not to be confused with Binary subtraction operator.**

Example Program

using System; namespace operators { class Program { static void Main(string[] args) { int x = 30; int y = 20; Console.WriteLine("***** Binary Arithmetic Operators *****\n"); Console.WriteLine("using + operator x + y = " + x + y); int z = x - y; Console.WriteLine("using - operator x - y = " + z); Console.WriteLine("using * operator x * y = " + x * y); Console.WriteLine("using / operator x / y = " + x / y); Console.WriteLine("using % operator x % y = " + x % y); Console.WriteLine("\n***** Unary Arithmetic Operators *****\n"); Console.WriteLine("using - unary operator on x = "+x +" i.e. -x = " + -x); Console.WriteLine("using ++ operator post x++ = " + x++); Console.WriteLine("using -- operator post x-- = " + x--); Console.WriteLine("using ++ operator pre ++x = " + ++x); Console.WriteLine("using -- operator pre --x = " + --x); } } }

**Relational**

- == For Equality
- != For not equality
- > For greater than
- < For less than
- >= Greater than equal
- <= Less than equal

**== Equal**

This operator is used to check equality of two values or operands. i.e. it will check that whether given values are equal or not. It can return true or false for one condition but not both at the same time.

E.g.

int x = 10; int y=20; int z=10; x == ywill return false as values for x and y are different. whilex == zwill return true as values for both are same.

**! = Not equal**

This operator is used to check whether two values are not equal. It is the reverse of Equality operator (==) Like equality operator this can also return true or false for one condition but not both at the same time.

E.g.

int x = 10; int y=20; int z=10; x! = ywill return true as values for x and y are different. whilex! = zwill return false as values for both are same.

** > greater than**

This operator is used to check if operand one is greater than operand two. It can return true or

false for one condition but not both at the same time.

E.g.

int x = 10; int y=20; int z=10; x > ywill return false as values for x is less than y. whiley > zwill return true as value for y is greater than z.

**< less than**

This operator is used to check if operand one is less than operand two. It can return true or

false for one condition but not both at the same time.

E.g.

int x = 10; int y=20; int z=10; x < ywill return true as values for x is less than y. whiley < zwill return false as value for y is greater than z.

**>= Greater than equal**

This operator is used to check if operand one is greater than or equal to operand two. It can return true or false for one condition but not both at the same time.

E.g.

int x = 10; int y=20; int z=10; x >= ywill return false as values for x is less than y. whiley >= zwill return true value for y is greater than z and x >= z will also return true as x and z have same values.

** <= Less than equal**

This operator is used to check if operand one is less than or equal to operand two. It can return true or false for one condition but not both at the same time.

E.g.

int x = 10; int y=20; int z=10; y <= zwill return false as values for y is not less than or equal to z. whilex <= ywill return true as value for x is less than y and x <= z will also return true as x and z have same values.

**Logical**

**&& Logical and****|| Logical or****! Logical not**

**&& Logical AND**

This operator is used to take logical AND of two operands. It will return true if both the operands are non-zero. If anyone of the two operands are zero then it will return false.

E.g.

int x = 0; int y=1; int z=1; x && ywill return false. whiley && zwill return true.

**|| Logical OR**

This operator is used to take logical OR of two operands. It will return true if any of the operands are true i.e. non-zero. If both operands are zero then it will return false.

E.g.

int x = 0; int y=1; int z=0; x || ywill return true similarlyy || zwill also return true. On the other hand,x || zwill return false.

**! Logical not**

This operator is used to take logical not of the operand. It will reverse the output of the operand. i.e. if operand is returning true it will make it false and vice versa.

**Note: – Relational and Logical Operators are mostly used with Conditional Statements.**

**Assignment**

**=** to assign a value

**+=** adds to previous value and assign it again to the same variable

**-= **subtracts from previous value and assign it again to the same variable

***=** multiplies with previous value and assign it again to the same variable

**/=** divide previous value and assign it again to the same variable

**%=** takes modulus and assign it to the same variable

**= Assignment**

This operator is used to assign values. it will assign right operand to left operand.

E.g.

int x; x=10;// this will assign 10 to x.int y; y=x;//this will assign value of x to y.

**+= Add AND assignment**

This operator is used to add left-hand operands value with right-hand operand value and then again assign it to left-hand side operand.

E.g.

int x = 10; int y = 20; x += y;// this will set 30 to x. it is exactly same asx = x + y;

**-= Subtract AND assignment**

This operator is used to subtract right hand operands value from left hand operand value and then again assign it to left hand side operand.

E.g.

int x = 10; int y = 20; x -= y;// this will set -10 to x. it is exactly same asx = x - y;

***= Multiply And assignment**

This operator is used to multiply left hand operands value with right hand operand value and then again assign it to left hand side operand.

E.g.

int x = 10; int y = 20; x *= y;// this will set 200 to x. it is exactly same asx = x * y;

**/= Divide AND assignment**

This operator is used to divide left hand operands value with right hand operand value and then again assign it to left hand side operand.

E.g.

int x = 10; int y = 20; x /= 20;// this will set 0 to x. it is exactly same asx = x / y;

**%= Modulus AND assignment**

This operator is used to take modulus left hand operands value with right hand operand value and then again assign it to left hand side operand.

E.g.

int x = 10; int y = 20; x %= y;// this will set 10 to x. it is exactly same asx = x % y;

Example Program

using System; namespace operators { class Program { static void Main(string[] args) { int x; int y = 20; int z = 10; Console.WriteLine("***** Arithmetic Assignment Operators *****"); x = z; Console.WriteLine("using = operator x = z : " + x); x += y; Console.WriteLine("using += operator x += y : " + x); x -= y; Console.WriteLine("using -= operator x -= y : " + x); x *= y; Console.WriteLine("using *= operator x *= y : " + x); x /= y; Console.WriteLine("using /= operator x /= y : " + x); x %= y; Console.WriteLine("using %= operator x %= y : " + x); } } }

**Output**

**Bitwise Operators**

These operators work on a single bit and perform the bitwise operation. It includes **&, | and ^**.

The truth table for these operators with 2 bits is shown below.

A | B | A & B | A | B | A ^ B |

0 | 0 | 0 | 0 | 0 |

0 | 1 | 0 | 1 | 1 |

1 | 0 | 0 | 1 | 1 |

1 | 1 | 1 | 1 | 0 |

**&**

It takes AND of all bits and returns 1 only when all of its bits are 1.

**|**

It takes OR of all bits and it returns 1 when any one of its bits is 1.

**^ **

It returns 1 when its bits are opposite. It only works with two bits at a time.