Operators in C#

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.

  1. Assignment Operator : =
  2. Arithmetic Operator : +,  -, *,  /,  %
  3. Comparison Operators : ==,  ! =, >, >=, <, <=
  4. Conditional Operators : &&,  ||
  5. 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.

Arithmetic Operator:

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

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 ‘/’.

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:

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.

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.

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.

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:

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!!!

 

Tagged

About dipneupane

Dip is a passionate Software Developer based in Kathmandu, Nepal. He chew code and smash keyboard for living. Apart from that, he writes about almost everything on buzzedcode.com.

View all posts by dipneupane →

Leave a Reply

Your email address will not be published. Required fields are marked *