Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C# - OPERATOR OVERLOADING


We have already seen that the + operatior is used to add two numbers.


Example :



class MyApplication
{
    public static void Main(string[] arg) 
    {
    	var x = 5;
	    var y = 7;
	    var z = x + y;
	    System.Console.WriteLine("The added result is : "+z);
    }
} 


Output :



  The added result is : 12

That was quite easy.


Now, let us say, we have a class that has an integer value. And we want to create two objects and add them.


Let me make it a little simpler with the below example.


Example :



class MyClass
{
    int x;
    
    public MyClass()
    {
    }
    
    public MyClass(int temp)
    {
        x = temp;
    }
}

class MyApplication
{
    public static void Main(string[] arg) 
    {
        var obj1 = new MyClass(5);
        var obj2 = new MyClass(7);
        var obj3 = obj1 + obj2;
        System.Console.WriteLine("The added result "+obj3);
    }
} 


Output :



  Operator '+' cannot be applied to operands of type 'MyClass' and 'MyClass'

So, in the above code, we have created a class named MyClass that has an attribute named x. And we have defined a Constructor that initialises the attribute x.


class MyClass
{
	int x;

	public MyClass()
	{
	}

	public MyClass(int temp)
	{
		x = temp;
	}
}

Then we have created an object obj1 of the class MyClass.


var obj1 = new MyClass(5);

And initialised the value of x with 5(For object obj1).

C_Sharp

Similarly, we have created a second object obj2.


var obj2 = new MyClass(7);

And initialised the value of x with 7(For object obj2).

C_Sharp

Now, we tried to add two objects obj1 and obj2. Just to add 5 and 7.


var obj3 = obj1 + obj2;

But we end up with the below error.


Operator '+' cannot be applied to operands of type 'MyClass' and 'MyClass'

This is because C# is confused that how can adding two objects add their values?


With this we come to the concept of Overloading the operators. i.e. We will make the + operator act the way, we want it to act.


Let us modify the above example.


Example :



class MyClass
{
    int x;
    
    public MyClass()
    {
    }
    
    public MyClass(int temp)
    {
        x = temp;
    }

    public static MyClass operator + (MyClass myClass1, MyClass myClass2) 
    {
		MyClass myClass = new MyClass();
		myClass.x = myClass1.x + myClass2.x;
        return myClass;
    }
    
    public void showResult()
    {
        System.Console.WriteLine("The added result is : "+x);
    }
}

class MyApplication
{
    public static void Main(string[] arg) 
    {
        var obj1 = new MyClass(5);
        var obj2 = new MyClass(7);
        var obj3 = obj1 + obj2;
        obj3.showResult();
    }
} 


Output :



  The added result 12

And all we have done is, defined a method called public static MyClass operator + (MyClass myClass1, MyClass myClass2) .


public static MyClass operator + (MyClass myClass1, MyClass myClass2)
{
	MyClass myClass = new MyClass();
	myClass.x = myClass1.x + myClass2.x;
	return myClass;
}

This operator + is already defined by C#. When we specify operator before the + sign. C# understands, the + needs to be Overloaded.


And all we are doing is, redefining the + sign. So that it works exactly the way we want it to work.


So, the public static MyClass operator + (MyClass myClass1, MyClass myClass2) has two parameters, MyClass myClass1 and MyClass myClass2.


Now, the operator + method gets called when the below statement executes.


var obj3 = obj1 + obj2;

And what happens is, the value of the object, obj1 is assigned to the first parameter (i.e. MyClass myClass1) and obj2 is assigned to the second parameter (i.e. MyClass myClass2).


And obj3 would be assigned to the value to be returned(i.e. MyClass myClass).

C_Sharp

And right now, myClass1 has the contents of obj1,

C_Sharp

And myClass2 has the contents of obj2,

C_Sharp

Now, since, the public static MyClass operator + (MyClass myClass1, MyClass myClass2) method has the contents,


public static MyClass operator + (MyClass myClass1, MyClass myClass2)
{
	MyClass myClass = new MyClass();
	myClass.x = myClass1.x + myClass2.x;
	return myClass;
}

So, the next line,


myClass.x = myClass1.x + myClass2.x;

Adds the value of obj1.x i.e. 5 and obj2.x i.e. 7.


And the added value is returned to obj3.


obj3 =  obj1 + obj2
C_Sharp


So, the next statement calls the method showResult,


obj3.showResult();

And the method showResult,


public void showResult()
{
	System.Console.WriteLine("The added result is : "+x);
}

Prints the value of x from the object obj3. And we get the below output.


The added result 12

But have you thought, when we are executing the statement,


var obj3 =  obj1 + obj2

How come the public static MyClass operator + (MyClass myClass1, MyClass myClass2) gets called?


This is because, for every operator, be it a + or - or any other operator, C# uses a method for each.


Similarly, for subtraction, multiplication and division C# has the below Overloaded method.


So, if you want to overload, any of the operators(Like -, *, /), you can define and customise it your own way.


Overloading '-' operator


Example :



class MyClass
{
    int x;
    
    public MyClass()
    {
    }
    
    public MyClass(int temp)
    {
        x = temp;
    }

    public static MyClass operator - (MyClass myClass1, MyClass myClass2) 
    {
		MyClass myClass = new MyClass();
		myClass.x = myClass1.x - myClass2.x;
        return myClass;
    }
    
    public void showResult()
    {
        System.Console.WriteLine("The subtracted result is : "+x);
    }
}

class MyApplication
{
    public static void Main(string[] arg) 
    {
        var obj1 = new MyClass(15);
        var obj2 = new MyClass(7);
        var obj3 = obj1 - obj2;
        obj3.showResult();
    }
} 


Output :



  The subtracted result is : 8