Operators in C# Programming

Operators are symbols that performs certain task in an expression. Operators in C# are special symbols like + / == . ++ etc. These operators can be used to process data. There operators are categorized below:

  1. Arithmetic Operators
  2. Logical Operators
  3. Bitwise Operators 
  4. Relational Operators 
  5. Assignment Operators
  6. String Concatenation Operator
  7. Type Conversion Operator
  8. Other Operators

They can also be categorized according to number of arguments. Operators that take one operand are unary, and those who take two operands are binary and those who take three operands are ternary.

Arithmetic Operators

Arithmetic Operators are operators used for mathematical calculation between two numerical values. They can perform addition, subtraction, modulus, increment etc. Some arithmetic operators are given below:

  • +
    This operator is used to perform addition between two operands.
  • -
    This operator is used to perform subtraction between two operands.
  • *
    This operator is used to perform multiplication between two operands.
  • /
    This operator is used to perform division between two operands. It divides numerator by denumerator and division by 0 is not valid.
  • %
    This operator is modulus operator and is used to find out remainder after integer division.
  • ++
    This operator is increment operator and is used to increase the value of integer by 1. When used after the integer variable, value is updated only in next statement but when used before the integer variable, value is stored updated right after increment.
  • --
    This operator is decrement operator and is used to decrease the value of integer by 1. When used after the integer variable, value is updated only in next statement but when used before the integer variable, value is stored updated right after decrement.

A program showing use of all Arithmetic Operators is given below:

using System;
    namespace Operators
    {
         class Program
         {
         static void Main(string[] args)
         {
             int a = 11;
             int b = 4;
             Console.WriteLine(a+b); //addition
             Console.WriteLine(a-b); //subtraction
             Console.WriteLine(a*b); //multiplication
             Console.WriteLine(a/b); //division
             Console.WriteLine(a%b); //modulus division
             Console.WriteLine(++a); //increment
             Console.WriteLine(--b); //decrement
             Console.ReadLine();
         }
         }
     }

Output:

15
7
44
2
3
12
3

Logical Operators

Logical Operators are operators that function on Boolean value types. It functions on true or false values. Boolean Operators in C# are negation (!), AND (&&), OR (||) and XOR (^). A truth table of these operators are given below:

a b !a a && b a || b a ^ b
T T F T T F
T F F F T T
F T T F T T
F F T F F F

Program showing use of logical operators are given below:

using System;
namespace Operators
{
    class Program
    {
        static void Main(string[] args)
        {
            bool a = true;
            bool b = false;
            Console.WriteLine(!a); // Negation
            Console.WriteLine(a && b); // AND
            Console.WriteLine(a || b); // OR
            Console.WriteLine(a ^ b); // XOR
            Console.ReadLine();
        }
    }
}

Output:

False
False
True
True

Bitwise Operators

Bitwise Operators work very similar to Logical Operators but they work in binary system. In computer system, all data are represented in 0's and 1's and these operators are used to modify them. Some bitwise operators are given below:

  • &
    This operator is binary AND operator. It sets the result to 1 if both operands are 1.
  • |
    This operator is binary OR operator. It sets the result to 1 if any one of two operands is 1.
  • ^
    This operator is binary XOR operator. It sets the result to 1 if only one of the two operand is 1.
  • ~
    This operator flips the bits.
  • <<
    This operator is binary left shift operator. It shifts one bit to left.
  • >>
    This operator is binary right shift operator. It shifts one bit to right.

Example to show use of these operators are given below:

using System;
namespace Operators
{
    class Program
    {
        static void Main(string[] args)
        {
            byte a = 1; //00000001
            byte b = 3; //00000011
            Console.WriteLine(a & b); //00000001 & 00000011
            Console.WriteLine(a | b); //00000001 | 00000011
            Console.WriteLine(a ^ b); //00000001 ^ 00000011
            Console.WriteLine(~a); //flips 00000001
            Console.WriteLine(a << 1); //shifts 00000001 one bit left
            Console.WriteLine(b >> 1); //shifts 00000011 one bit right
            Console.ReadLine();
        }
    }
}

Output:

1
3
2
-2
2
1

Relational Operators

Relational Operators are operators that are used to compare two operands and return a Boolean result. Some relational operators are listed below:

  • >
    This operator is used to check if left operand is greater than right operand or not.
  • >=
    This operator is used to check if left operand is greater than or equal to right operand or not.
  • <
    This operator is used to check if left operand is less than right operand or not.
  • <=
    This operator is used to check if left operand is less than or equal to right operand or not.
  • ==
    This operator is used to check if two operands are equal or not. If equal, it is true.
  • !=
    This operator is used to check if two operands are equal or not. If not equal, it is true


Example to show use of relational operators are given below:

using System;
namespace Operators
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 21, y = 22;
            Console.WriteLine(x > y); // Greater than
            Console.WriteLine(x >= y); // Greater than or equal to
            Console.WriteLine(x < y); // Less than
            Console.WriteLine(x <= y); // Less than or equal to
            Console.WriteLine(x == y); // Is it equal
            Console.WriteLine(x != y); // Is it not equal
            Console.ReadLine();
        }
    }
}

Output:

False
False
True
True
False
True

Assignment Operators

Assignment Operators are used to assign value to a variable. It can be done by using of '=' operator. We can also assign value more than once in a single expression using '=' operator more than once. This is known as cascade assignment. We can also use two operators together to perform certain task in smaller amount of codes. They are known as compound assignment operators. Example: +=, /= etc. Some assignment operators are listed below:

  • =
    This is a simple assignment operator which assigns value from right to variable in left.
  • +=
    This is add and assign operator, It adds right operand to left operand and store result in left operand.
  • -=
    This is subtract and assign operator, It subtracts right operand from left operand and store result in left operand.
  • *=
    This is multiply and assign operator, It multiplies two operands and store result in left operand.
  • /=
    This is divide and assign operator, It divides left operand with right operand and store result in left operand.
  • %=
    This is modulus and assign operator, It finds modulus of two operands and store result in left operand.
  • <<=
    This is left shift and assign operator. It shifts the operand on left to left with number in right operand and store value in left operand.
  • >>=
    This is right shift and assign operator. It shifts the operand on left to right with number in right operand and store value in left operand.
  • &=
    This is bitwise AND and assign operator. It computes bitwise AND of two operands and stores the result in left operand.
  • |=
    This is bitwise OR and assign operator. It computes bitwise OR of two operands and stores the result in left operand.
  • ^=
    This is bitwise XOR and assign operator. It computes bitwise XOR of two operands and stores the result in left operand.

Example showing use of assignment operators are given below:

using System;
namespace Operators
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b, c;
            a = b = 5; //cascade assignment
            c = 6; //normal assignment
            c += b; //use of compound assignment operator
            Console.WriteLine(a);
            Console.WriteLine(b);
            Console.WriteLine(c);
            Console.ReadLine();
        }
    }
}

Output:

5
5
11

String Concatenation Operator

String Concatenation Operator is used for joining two or more strings. It is simply done with '+' operator. We can also use compound assignment add operator to perform string concatenation. Example for understanding string concatenation is given below:

using System;
namespace Operators
{
    class Program
    {
        static void Main(string[] args)
        {
            string first = "joining ";
            string second = "three ";
            string third = "strings";
            string whole = first + second + third; //string concatination
            Console.WriteLine(whole);
            whole += " done"; //string concatination with compound assignment add operator
            Console.WriteLine(whole);
            Console.ReadLine();
        }
    }
}

Output:

joining three strings
joining three strings done

Type Conversion Operator

Type Conversion Operators deal with conversion of data types. It is used to convert variables from one type to another. Some of type conversion operator are listed below: 

  • as
    This operator is used to cast. If the cast fails it doesn't raise any exception.
  • is
    This operator is used to check object type.
  • sizeof
    This operator is used to find the size of data type.
  • typeof
    This operator is used to find the type of class.

Example showing the use of type conversion operator is given below:

using System;
namespace Operators
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(sizeof(int));
            Console.WriteLine(typeof(Program));
            Console.ReadLine();
        }
    }
}

Output:

4
Operators.Program

Other Operators

There are some other operators left in C#. These operators are listed below:

  • &
    This operator is used to return address of a variable.
  • *
    This operator is used to create a pointer variable.
  • ?:
    This operator is used to return one of two value according to the provided condition.
  • .
    This operator is used to access a member or a method.

Example of use of some of there operators is given below:

using System;
namespace Operators
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 4;
            Console.WriteLine((a % 2 == 0) ? "even" : "odd");
            Console.ReadLine();
        }
    }
}

Output:

even