Tuesday, 29 January 2013

Operator Overloading in C Sharp

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.

usingSystem;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

publicpartialclass_Default :
System.Web.UI.Page

{

protectedvoid
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:-

usingSystem;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

publicpartialclass_Default :
System.Web.UI.Page

{

protectedvoid
Page_Load(object sender, EventArgs e)

{

Calculator cal1 = newCalculator();

cal1.x = 10;

cal1.y = 20;

Calculator cal2 = newCalculator();

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:-

usingSystem;

using System.Collections.Generic;

using System.Linq;

using System.Web;

///<summary>

/// Summary description for Calculator

///</summary>

publicclassCalculator

{

publicint x;

publicint y;

publicstaticCalculatoroperator
+(Calculator cal1, Calculator
cal2)

{

Calculator cal = newCalculator();

cal.x
= cal1.x + cal2.x;

cal.y
= cal1.y + cal2.y;

return cal;

}

}

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

usingSystem;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

publicpartialclass_Default :
System.Web.UI.Page

{

protectedvoid
Page_Load(object sender, EventArgs e)

{

Calculator cal1 = newCalculator();

cal1.x = 10;

cal1.y = 20;

Calculator cal2 = newCalculator();

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:-

usingSystem;

using System.Collections.Generic;

using System.Linq;

using System.Web;

///<summary>

/// Summary description for Calculator

///</summary>

publicclassCalculator

{

publicint x;

publicint y;

//binary opertor overloading where we pass 2 parameter

publicstaticCalculatoroperator
+(Calculator cal1, Calculator
cal2)

{

Calculator
cal = newCalculator();

cal.x
= cal1.x + cal2.x;

cal.y
= cal1.y + cal2.y;

return cal;

}

//unary operator overloading

publicstaticCalculatoroperator
++(Calculator cal1)

{

Calculator cal = newCalculator();

cal.x
= cal1.x++;

cal.y
= cal1.y++;

return cal;

}

}

Similarly we can operate (--) operator.

Operator overloading of object(ref
type) and Value type:-

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

publicstaticCalculatoroperator
+(Calculator cal1, int
x)

{

Calculator cal = newCalculator();

cal.x
= cal1.x + x;

cal.y
= cal1.y + x;

return cal;

}

Calling of this operator overloading:-

Calculator cal1 = newCalculator();

cal1.x = 10;

cal1.y = 20;

Calculator calwithVar = cal1 + 3;

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

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