Operators in C#

Introduction

In a computer language, operators are very important as we perform operations in our program with the help of these operators only. Let us take a small example. Consider the below-mentioned line:

c = a + b;

Here, we have used 2 operators, the first one is ‘+’ and the second one is ‘=’. Confused, why am I saying that the first operator is + even though we have written ‘=’ before that. Have I gone mad? Probably no, because in the above instruction given to the computer, the computer will first have to calculate the sum of variables a and b and only then will it assign that value to the variable named c.

Here, + is the arithmetic operator and = is the assignment operator. Remember that + and = in the above line are called the operators and the variables a, b and c are called the operands. In simple terms operators operate on operands to get to some result for the computer to understand.

You will notice that + operator has a different functionality than the = operator, and what is that? The + operator helps in getting the sum of the left-hand side operand and the right-hand side operand, whereas the = operator helps in assigning the right-hand side value to the left-hand side operand.

Operator is symbol which are used to perform some action on two or more variables. C# has built-in set of various operators.

Below is Built-in type operators in C#:

  • Arithmetic Operators
  • Logical Operators
  • Relational Operators
  • Assignment Operators
  • Bitwise Operators

Arithmetic Operators:

Arithmetic operators are used to performing arithmetic operations such as addition, subtraction, multiplication, division, etc. There are two types of arithmetic operators: Binary and Unary. The arithmetic operators are called binary operators when they operate on two operands. Below mentioned are the various binary arithmetic operators available to us in C#:

OperatorOperator Name
+Addition Operator
Subtraction Operator
*Multiplication Operator
/Division Operator
%Modulo Operator (Remainder)

For example

using System;

namespace MyFirstApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 10;
            int b = 3;
            int result;
            result = a % b;
            Console.WriteLine("The remainder is: " + result);
            Console.ReadLine();
        }
    }
}

Logical Operators

Logical operators combine two or more conditions and evaluate a logical decision such as and, or. Logical operators return boolean values.

OperatorOperator Name
&&Logical AND
||Logical OR
!Logical NOT

For example

using System;

namespace MyFirstApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            bool Condition1 = true;
            bool Condition2 = true;
            Console.WriteLine("Testing the && Operator");
            if (Condition1 == true && Condition2 == true)
            {
                Console.WriteLine("Both Conditions are true");
            }
            //Make condition1 to be false
            Condition1 = false;
            Console.WriteLine("Making Condition1 = false");
            if (Condition1 == true && Condition2 == true)
            {
                Console.WriteLine("Both Conditions are true");
            }
            else
            {
                Console.WriteLine("Both Conditions are not true");
            }
            Console.WriteLine(); //Have written this statement to just print a blank line
            Console.WriteLine("Testing the || Operator");
            if (Condition1 == true || Condition2 == true)
            {
                Console.WriteLine("Condition1 or Condition2 or both are true");
            }
            //Make condition2 to be false
            Condition2 = false;
            Console.WriteLine("Making Condition2 = false");
            if (Condition1 == true || Condition2 == true)
            {
                Console.WriteLine("Condition1 or Condition2 or both are true");
            }
            else
            {
                Console.WriteLine("Both Conditions are false");
            }
            Console.WriteLine(); //Have written this statement to just print a blank line
            Console.WriteLine("Testing the ! Operator");
            if (!Condition1 == true) //Since Condition1 is currently false, !false will be equal to true
            {
                Console.WriteLine("Condition1 is false");
            }
            Console.ReadLine();
        }
    }
}

Relational Operators

Relational operators are used to check the relationship between two operands or calculate a relation between two operands Relational Operators return boolean values.

OperatorOperator Name
==Equal To
!=Not Equal To
Greater than
Less Than
>=Greater Than Equal To
<=Less Than Equal To

For example

using System;

namespace MyFirstApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            bool result;
            int x = 2, y = 8;
            // Equal to Operator
            result = (x == y);
            Console.WriteLine("Equal to Operator: " + result);
            // Greater than Operator
            result = (x > y);
            Console.WriteLine("Greater than Operator: " + result);
            // Less than Operator
            result = (x < y);
            Console.WriteLine("Less than Operator: " + result);
            // Greater than Equal to Operator
            result = (x >= y);
            Console.WriteLine("Greater than or Equal to Operator: " + result);
            // Less than Equal to Operator
            result = (x <= y);
            Console.WriteLine("Less than or Equal to Operator: " + result);
            // Not Equal To Operator
            result = (x != y);
            Console.WriteLine("Not Equal to Operator: " + result);
            Console.ReadLine();
        }
    }
}

Assignment Operators

These operators are used to assign values to variables with different operations.

OperatorOperator Name
 =assigns
+=adds the value of the variable on the left with the value on the right.
-=subtracts the value of the variable on the left with the value on the right.
*=multiplies the value of the variable on the left with the value on the right.
/=divides the value of the variable on the left with the value on the right.

For example

using System;

namespace MyFirstApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 2; //This is the simple assignment operator
            int b = 3; //This is the simple assignment operator
            Console.WriteLine("Vlaue of a+=b is: " + (a += b));
            Console.WriteLine("Vlaue of a-=b is: " + (a -= b));
            Console.WriteLine("Vlaue of a*=b is: " + (a *= b));
            Console.WriteLine("Vlaue of a/=b is: " + (a /= b));
            Console.WriteLine("Vlaue of a%=b is: " + (a %= b));
            Console.WriteLine("Vlaue of a<<=b is: " + (a <<= b));
            Console.WriteLine("Vlaue of a>>=b is: " + (a >>= b));
            Console.WriteLine("Vlaue of a&=b is: " + (a &= b));
            Console.WriteLine("Vlaue of a^=b is: " + (a ^= b));
            Console.WriteLine("Vlaue of a|=b is: " + (a |= b));
            Console.ReadLine();
        }
    }
}

Bitwise Operators

Bitwise operators are used to perform bit manipulation operations.

OperatorDescription
&Bitwise AND
|Bitwise OR
^Bitwise XOR
<< Bitwise Left Shift
>> Bitwise Right Shift

For example

using System;

namespace MyFirstApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 11;
            int b = 12;
            int c;
            Console.WriteLine("Value of a is: " + a);
            Console.WriteLine("Value of b is: " + b);
            Console.WriteLine();
            c = a & b;
            Console.WriteLine("Value of a & b is: " + c);
            c = a | b;
            Console.WriteLine("Value of a | b is: " + c);
            c = a ^ b;
            Console.WriteLine("Value of a ^ b is: " + c);
            Console.WriteLine();
            Console.WriteLine("Value of ~a is: " + ~a);
            Console.WriteLine("Value of ~b is: " + ~b);
            Console.WriteLine();
            Console.WriteLine("Understanding bitwise left and right shift operators");
            Console.WriteLine("Left Shifting a by 1 bit, the value becomes: " + (a << 1));
            Console.WriteLine("Right Shifting a by 1 bit, the value becomes: " + (a >> 1));
            Console.ReadLine();
        }
    }
}

Leave a Reply

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