by Dinesh Thakur

Another important and exciting feature object-oriented programming is Operator overloading. C# supports the concept of operator overloading.

Operator overloading is a concept in which operator can defined to work with the userdefined data types such as structs and classes in the same way as the pre-defined data types.

Consider an example:

 

Overloading on '+' operator

 

using System;

public class Item

{

pub1ic int i;

public Item( int j)

{

i = j;

}

public static Item operator + ( Item x , Item y)

-{

system.console.writeLine("operator +" + x.i +" "+ y.i);

Item z = new Item(x.i+y.i);

return z;

}

}

public class Test

{

public static void Main()

{

Item a = new Item(l0);

Item b = new Item(5);

Item c;

c = a + b ;

system.console.writeLine(c.i);

}

}

OUTPUT:

Operator + 10 5

15

The word operator as the name of a function, is legal and the only way to overload operators. We follow this word with the operator we want to overload and then the parameters we will call the operator with. + is a binary operator and will need two Item's, one on its left and the other on its right. Then at beginning, we give the return value. of the operator. In our case we want a + to add two Item's and return a third Item where i will be the sum of the individual i's. Thus a+b will call the operator + with x being equal to a and y to b. Thus x.i will have a value 10 and y.i, 5. We are creating a new object z and in the constructor passing 15 i.e. 10 + 5. Thus the i of z will be 15 which is being returned. a + b will now be replaced by the object whose i has a value 15 and c will be equal to this object. Thus c.i will be equal to 15.

There are many operators that cannot be overloaded. Which are listed below:

• Conditional Operator                    & &, II

• Compound Assignment                +=, -=, *=, 1=, %=

• Other Operators                            [], 0, = , ?:, ->, new, sizeof, typesof, is, as

Multiple Calling of Overloaded Operator.

 

using system;

public class Item

{

pub1ic int i;

public Item( int j)

{

i = j;

}

public static Item operator + ( Item x , Item y)

{

system.console.writeLine(“operator +" + x.i +u " + y.i);

Item z = new Item(x.i+y.i);

return z;

}

}

public class Test

{

public static void Main()

{

Item a = new Item(lO);

Item b = new Item(5);

Item c = new Item(2);

Item d;

d = a'+ b + c ;

system.Console.writeLine(d.i);

}

}

OUTPUT:

Operator + 10 5

Operator + 15 2

17

There is only change d = a + b + c from above example. C# gets easily confused with complex statements so it does not read all of it. It sees two operators on the same line. In this case, the same plus. An internal rule tells it to read the plus left to right i.e. it will only see a + b. It will call the operator + with x.i as 10 and y.i as 5 because a's i is 10 and obi’s i is 5. This will create a temporary object like Item whose i is 15, lets call it zz. The object z is very different from zz. C# then evaluates zz + c. Thus x.i will display 15 and y.i will have the value of c.i i.e. 2. To support multiple invocations of the operator on a single line, the code does not change.