Operator Overloading

Before understanding the operator overloading we need to understand why we use operator overloading. In the following example we have one class calculator which contains two variable x and y. and one web form where we create two variables in its cs file and add them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int p = 10;
        int q = 20;
        int r = p + q;
    }
}

		

In this operation we didn’t get any problem but if we try to add two objects we will get error like this:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Calculator cal1 = new Calculator();
        cal1.x = 10;
        cal1.y = 20;

        Calculator cal2 = new Calculator();
        cal2.x = 15;
        cal2.y = 25;

        //we will get error that we cannot use + operator of type Calculator(which is ref type)
        Calculator cal = cal1 + cal2;
    }
}
	

In this situation we have to give the new definition to the + operator so that we can perform this operation. This can be done by operator overloading.

To overload the operator we have to use the operator keyword and declared the static method like this:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class Calculator
{
    public int x;
    public int y;

    public static Calculator operator +(Calculator cal1, Calculator cal2)
    {

        Calculator cal = new Calculator();
        cal.x = cal1.x + cal2.x;
        cal.y = cal1.y + cal2.y;
        return cal;
        
    
    }
}

After overloading the + operator we can perform the following operation:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Calculator cal1 = new Calculator();
        cal1.x = 10;
        cal1.y = 20;

        Calculator cal2 = new Calculator();
        cal2.x = 15;
        cal2.y = 25;

     
        Calculator cal = cal1 + cal2;
        Response.Write(cal.x);
        Response.Write(cal.y);
    }
}

		

This is the example of binary operator overloading.

Overloading of Unary operator

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class Calculator
{
    public int x;
    public int y;

    //binary opertor overloading where we pass 2 parameter
    public static Calculator operator +(Calculator cal1, Calculator cal2)
    {

        Calculator cal = new Calculator();
        cal.x = cal1.x + cal2.x;
        cal.y = cal1.y + cal2.y;
        return cal;
        
    
    }

    //unary operator overloading
    public static Calculator operator ++(Calculator cal1)
    {

        Calculator cal = new Calculator();
        cal.x = cal1.x++;
        cal.y = cal1.y++;
        return cal;


    }


}

		

Similarly we can overload (--) operator.

int[] data = new int[] { 3, 4, 5, 6, 9 };

		

Another Example

Operator overloading of object(ref type) and Value type

Suppose you want to add int value in object. Then we perform the following overloading:-

public static Calculator operator +(Calculator cal1, int x)	
    {

        Calculator cal = new Calculator();
        cal.x = cal1.x + x;
        cal.y = cal1.y + x;
        return cal;


    }

		

Calling of this operator overloading:-

Calculator cal1 = new Calculator();
        cal1.x = 10;
        cal1.y = 20;

Calculator calwithVar = cal1 + 3;

		

Comparison Operator

Overloading of Comparison operator like(==, !==)

If you want to comparison operator like == then we must have to overload its complementary operator which is !=.

public static bool operator ==(Calculator cal1, Calculator cal2)
    {

        if ((cal1.x == cal2.x) && (cal1.y == cal2.y))
        {

            return true;

        }
        else
        {

            return false;
        
        }



    }
    public static bool operator !=(Calculator cal1, Calculator cal2)
    {

        if ((cal1.x != cal2.x) || (cal1.y != cal2.y))
        {

            return true;

        }
        else
        {

            return false;

        }



    }


		

Calling of this operator overloading:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Calculator cal1 = new Calculator();
        cal1.x = 10;
        cal1.y = 20;

        Calculator cal2 = new Calculator();
        cal2.x = 15;
        cal2.y = 25;

        if (cal1 == cal2)
        {

            Response.Write("both are equal");

        }
        else
        {


            Response.Write("both are not equal");
        }
    }
}

		

Email Address

For any query you can send mail at info@techaltum.com
Thanks