How to use Operator Overloading in C#

In this article, we will discuss operator overloading in C#. It means that we are defining an operator for a class. It is very like method overloading. We can declare it with the help of the operator keyword like this:

public static calculation operator /(calculation op1, calculation op2)
{
}

Here we define an operator (/) with the operator keyword.

Declaration: We can generally declare it this way:

For Unary

public static ret-type operator  op(parameter-type operand)
{
}

For Binary

public static ret-type operator  op(parameter-type operand1, parameter-type operand1)
{
}

Example

First we look at an example of a unary operator(++):

class calculation
{
   int a, b, c;
   public calculation()
   {
       a = b = c = 0;
   }
   public calculation(int x, int y, int z)
   {
       a = x;
       b = y;
       c = z;
   } 
   public static calculation operator ++(calculation op1)
   {
       op1.a++;
       op1.b++;
       op1.c++;
       return op1;
   }
   public void ShowTheResult()
   {
       Console.WriteLine(a + "," + b + "," + c);
       Console.ReadLine();
   }
}
class Program
{
    static void Main(string[] args)
    {
        calculation i = new calculation(10, 20, 30);
        calculation j = new calculation(5, 10, 3);
        calculation k = new calculation();
        i++;
        i.ShowTheResult();
        Console.WriteLine();
    }
}

The output will be

Clipboard01.gif

Here we use the ++ operator so the values 10, 20, 30 will be 11, 21, 31.

Now we look at an example of a binary operator; here we do the calculations like (+,-,*,/) with the help of operator overloading.

Here we create a Class Calculation in our program like this:

class calculation
{
     int a, b, c;
     public calculation()
     {
         a = b = c = 0;
     }
     public calculation(int x, int y, int z)
     {
         a = x;
         b = y;
         c = z;
     }
     public static calculation operator +(calculation op1, calculation op2)
     {
         calculation calc = new calculation();
         calc.a = op1.a + op2.a;
         calc.b = op1.b + op2.b;
         calc.c = op1.c + op2.c;
         return calc;
    }
    public static calculation operator -(calculation op1, calculation op2)
    {
         calculation calc = new calculation();
         calc.a = op1.a - op2.a;
         calc.b = op1.b - op2.b;
         calc.c = op1.c - op2.c;
         return calc;
    }
    public static calculation operator *(calculation op1, calculation op2)
    {
         calculation calc = new calculation();
         calc.a = op1.a * op2.a;
         calc.b = op1.b * op2.b;
         calc.c = op1.c * op2.c;
    }
    public static calculation operator /(calculation op1, calculation op2)
    {
         calculation calc = new calculation();
         calc.a = op1.a / op2.a;
         calc.b = op1.b / op2.b;
         calc.c = op1.c / op2.c;
         return calc;
    }  

    public void ShowTheResult()
    {
         Console.WriteLine(a + "," + b + "," + c);
         Console.ReadLine();
    }
}

Here we define the operators and a function (ShowTheResult()) to show the output. After that we write the following code in the Program class. By this the addition will be performed and the result will be shown with the help of The ShowTheResult() function. 

class Program
{
    static void Main(string[] args)
    {
        calculation j = new calculation(5, 10, 3);
        calculation k = new calculation();
        k = i + j;
        k.ShowTheResult();
        Console.WriteLine();
    }
}

Output

2.gif

For subtraction, multiplication and division we write the following code:

class Program
{
     static void Main(string[] args)
     {
         calculation i = new calculation(10, 20, 30);
         calculation j = new calculation(5, 10, 3);
         calculation k = new calculation();
         k = i - j;
         k.ShowTheResult();
         Console.WriteLine();          
     }
}

Multiply

class Program
{
     static void Main(string[] args)
     {
         calculation i = new calculation(10, 20, 30);
         calculation j = new calculation(5, 10, 3);
         calculation k = new calculation();
         k = i * j;
         k.ShowTheResult();
         Console.WriteLine();
     }
}

Division

class Program
{
     static void Main(string[] args)
     {
         calculation i = new calculation(10, 20, 30);
         calculation j = new calculation(5, 10, 3);
         calculation k = new calculation();
         k = i / j;
         k.ShowTheResult();
         Console.WriteLine();
    }
}


Similar Articles