Thursday, December 6, 2018
Home > C# > Operators in C#

C# operators

We can define c# operators as elements which we can apply to one or more operands in a statement. Operators in c# usually work with one or two operands. But there is a well-known operator in c# called Ternary operator which works with three operands at a time.

Types of Operators in c#

Different types of C# operators are:

Arithmetic

Arithmetic operators are of two types:

  1. Binary
  2. 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.

     The 2+3 result will be 5.
     10+20 result will be 30.
     It can be used as 10+20+30. In this case, it will add 10 & 20 then the result will be added
     to 30, and the result will be 60.

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

     5-3 result will be 2.
     10-20 result will be -10.
     It can be used as 10-20-30. In this case, it will subtract 20 from 10 and then 30 will be
     subtracted from the result. Final result will be -40.

* Multiply
This operator multiplies two values.

     2*3 will give us 6.
     15*2 will give us 30.
     10*20*30 will give us 6000. It will first multiply 10 & 20 and the result will be multiplied 
     to 30.

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

     10/5 will give us 2. 
     5/10 will give us 0. 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/ 5 will give us 0. 
     While 5/10 will give us 5. Because remainder will be 5.

Unary (Single operand)

As the name suggests unary operators in c# work with single operands. They include

  1. ++ increment
  2. — decrement
  3. – 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:

     Let x = 10 and 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:

     Let x = 10 and its post-increment will be x++.

– – 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:

     Let x = 10 and 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:

     Let x = 10 and its post-decrement will be x--.

– Minus
It is used to take negative of an operand.
E.g.

     Let x = 10; then –x will 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);

        }
    }
}

Output
C# operators - arithmetic operators example

Relational

C# rational operators include

  1. == For Equality
  2. != For not equality
  3. > For greater than
  4. < For less than
  5. >= Greater than equal
  6. <= 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 == y will return false as values for x and y are different.
     while x == z will 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! = y will return true as values for x and y are different.
     while x! = z will 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 > y will return false as values for x is less than y.
     while y > z will 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 < y will return true as values for x is less than y.
     while y < z will 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 >= y will return false as values for x is less than y.
     while y >= z will 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 <= z will return false as values for y is not less than or equal to z.
     while x <= y will return true as value for x is less than y and x &lt= z will also return 
     true as x and z have same values.

Logical

Logical operators in c# are used to perform logical operations in the programs.

  1. && Logical and
  2. || Logical or
  3. ! 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 is zero then it will return false.
E.g.

     int x = 0;
     int y=1;
     int z=1;
     x && y will return false.
     while y && z will 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 || y will return true similarly y || z will also return true.
     On the other hand, x || z will 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 an 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 the 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 as x = 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 as x = 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 as x = 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 as x = 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 as x = 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
operators in c# - arithmetic assignment operators example
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.

 

ABA & BA | BA ^ B00000010111001111110

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

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.