Operator Overloading

C#, like its ancestor (C++), allows you to overload operators. Overloading an operator means making an operator (for example the addition operator, +) behave differently. We may want the addition operator to behave differently when we apply it on certain objects of classes or structs. To understand the need for operator overloading first let us figure out why we need operators in the first place. Can we not perform addition of two integer variables using methods? Consider Example 7.

Example 7:

...
int result = Int.Add(54, 200);
int result2 = 54 + 200;
...

Consider the first line of code in Example 7. The variable result is calculated using the Add method of the Int class. The second line calculates the result using the addition operator (the plus “+” symbol). As you can see, both accomplish the same task. Then why have operators? An entire language cab be created which does not use operators at all! Almost everything that operators do can be achieved through methods.

The reason we use operators is to make our equations look simple and easy to understand. In Example 7 we have to add just two integers. Imagine a quadratic equation, or a formula to calculate the distance between a star and Earth using methods! Already these formulas are complex and with the operators missing and more text added (as methods) they would almost become impossible to decipher!

Operators are used for better readability of code as it makes code more concise. Similarly, as our programs get complex we have a plenty of classes, which means plenty of objects. This in turn means plenty of operations to be performed on these objects. It is in this kind of situation where it pays to overload an operator. Overloading an operator eases the operations performed upon objects of these classes. As seen operators achieve the same goal as methods do, only in a much more concise and a neater way.

However, not all operators can be overloaded. Below Table lists out the operators that can be overloaded.

+ ! ~ ++
* / % & | ^
<< >> != >< <= >=

The process of overloading an operator is very much similar to that of creating a method. All overloaded operators are static methods.

Consider Example 8,

Example 8:

Implementing OOP Concepts in C# Example 8

The output of Example 8 will be,

Finish time would be : 13 hours and 40 minutes.

In Example 8 we have

  • Declared a struct by the name Time, which has two data members hours and minutes.
  • Overloaded the + (addition) operator such that if objects of struct Time are passed to it, then the operator should add the hours and minutes and return another object of type Time. The syntax is discussed later.
  • Declared three objects of type Time in the Main() function. We have set the hours and minutes of the start and duration object. The finish object makes use of the overloaded operator + to add the hours and minutes of the other two objects.
  • Displayed the hours and minutes of the finish object to the user, which is nothing but the result of addition of the start and duration objects added up.

We shall now take a look at the steps involved in overloading an operator in greater detail.

The syntax to overload an operator is shown below (it is an extract from Example 8 for your easier understanding purpose)

public static Time operator +(Time first, Time second)
{
     return new Time(first.hours + second.hours, first.minutes + second.minutes);
}

As you can see the syntax is very similar to that of declaring a method.

  • You begin by specifying the access-modifier. In our example we have used the public access modifier.
  • The keyword static makes the operator common to the class and not to a specific object. This is mandatory.
  • Next we define the return type of the operator. Return types pertain to the result of the operator. In our example the return type is Time.
  • After the return type the keyword operator is specified. This is mandatory.
  • The operator to be overloaded is specified next. Make sure it is only one of the operators specified in table above. In our example we have overloaded the + operator.
  • Next we specify the parameters that the operator should operate upon. In our example, it tis two objects of type Time.
  • We begin the method, just as any other method, using curly braces and writing code within it. In our example, we simply add the hours and minutes of the two objects passed and return it as another Time object.

Leave a comment