In general, Operator is useful to perform the operation between or among the operands. Let’s, have a quick example.

eg: a+b=c

Here ‘a’ and ‘b’ are operands and ‘+’ is operator. The plus (+) sign perform the operation between the operands to generate the result c. In C#, there are various operators which are listed as below.

- Assignment Operator :
**=** - Arithmetic Operator :
**+, -, *, /, %** - Comparison Operators :
**==, ! =, >, >=, <, <=** - Conditional Operators :
**&&, ||** - Ternary Operator :
**?:**

Each operators are described as below.

__Assignment Operator:__

Generally this operator is used to assign the value to the variable of any type. Let’s suppose, I want to assign the age, name and ispassed value then it can be used like this.

1 2 3 |
string name="Dip Neupane"; int age=23; bool ispassed=true; |

__Arithmetic Operator:__

These operators are simply used to perform the arithmetic operations. Let’s suppose, I want to add the two numbers then,

1 2 |
int a = 5; int b = 2; int c = a + b; |

Here, I have used assignment and arithmetic operators in the same line of code. You can use other arithmetic operators (-,*,/,%) in the same way.

**Difference between ‘/’ and ‘%’**

Suppose we have two number 10 and 5 and let’s see using these two different arithmetic operators vary our result. First let’s use ‘/’.

1 2 |
if 10/5, the result is 2(quotient) and if 10%2, the result is 0(reminder). |

__Comparison Operator:__

These operators are use to perform the comparison. During code, we need to perform the action depending upon our conditions. So In these conditions we use comparison operators:

1 2 3 4 5 6 7 8 9 10 |
int a = 0; if (a == 5) { Console.WriteLine("The number is five."); } else { Console.WriteLine("The number is not five."); } Console.ReadLine(); |

The ‘! =’ operator check the ‘not equal’ condition. The other comparison operators can be used in similar way.

__Conditional Operator:__

Generally when we need to compare the two conditions these operators come handy. Let’s say two numbers.

1 2 3 4 5 6 |
//use of &&(and) operator int a = 5, b = 10; ; if (a == 5 && b == 10) { // some sample code } |

In this case both the conditions (a==5) and (b==10) should be satisfied. Otherwise the code written inside the if block can not be executed.

1 2 3 4 5 6 |
//use of ||(or) operator int a = 0, b = 1; if (a == 5 || b == 10) { //some sample code } |

In this case, If any one of these two conditions (a==5) and (b==10) satisfied, the code written inside the if block will be executed.

__Ternary Operator:__

This is one of the interesting operator in c# program. Before describing about this operator let’s examine the simple example.

1 2 3 4 5 6 7 8 9 10 11 12 |
int a = 5; bool isFive; if (a == 5) { isFive = true; } else { isFive = false; } Console.WriteLine("Number == 5 is {0}", isFive); Console.ReadLine(); |

Here I have simply two variable. First is int type “a” assigned with number 5 and the second one is ‘isFive’ which is bool which store either true or false. Now let’s perform the same above task using Ternary operator:

1 2 3 4 5 |
int a = 5; bool isFive; isFive = a == 5 ? true : false; Console.WriteLine("Number == 5 is {0}", isFive); Console.ReadLine(); |

Here whatever follows the “?” represents the true condition and whatever follows the “:” part represent the false condition.

Now check these above two piece of code separately in your demo c# console application. You will get the same result. So the comparison done in first code (5 lines) can be replaced with just a single line of code written in the below example. This is the beauty of ternary operator. Cool!!!