C# Operator Overloading
Operator overloading is one of the most fascinating and interesting object oriented programing concept which was first integrated in C++ language. As we know that C# integrates the robustness of C++ with delicacies of Java; therefore it has also inherited the concept of operator overloading from C++.
If you have been integrating object oriented principles while writing programs, you should have an idea about method overloading. In method overloading we write different methods with same name to achieve different functionality from them. Operator overloading extends this concept to C# operators. With operator overloading you can add, subtract and perform virtually all functionality on objects which you achieve with ordinary operators. For example, you can add two objects like
Object o1, o2, o3;
o3= o1 + o2;
This article has been divided into three sections. In the first section, you will learn how you can overload operators to operate on two objects of custom data types. In the following section, we have extended the concept and have explained how overloaded operators can operate on custom and primitive data types simultaneously and in the last and final section we have described how operator overloading can be used for data conversion purposes, we have described with the help of example, how you can convert primitive data types to user defined or custom data types and vice-versa. Now, let us get straight to the business.
There are some rules that must be followed while overloading an operator.
Next lines are important in ClassA. We have inserted an operator function which actually takes two objects of ClassA as parameter and then perform the addition of their num variable. The result of the addition has been passed into another anonymous object as parameter and then it is returned back to the calling function. While declaring this operator function we have followed all the rules which we described earlier. We have made the function public static. Also, the operands have been passed as the parameters. We said of all the parameters that are passed to the operators, at least one of them should be that type in which the operator function has been defined. In our operator function both of the parameters are of type ClassA which is also the class in which operator function has been defined.
When compiler reached this point of code, it will first check that weather the types on the left and right of the operator are primitive types or custom types. If the types are primitive such as integer or float, it will simply add them by calling some built in arithmetic routine. However if the compiler finds that the types on the left and right of operator are custom types, it will check that if at least one of the types surrounding the operator have corresponding operator function declared in the body or not. When it finds the operator function it will pass the surrounding types as parameter to that operator function and then execute the operator function.
We have declared that the return type of the operator would be ClassA type object containing the variable num equal to the sum of the num variables of the two ClassA objects passed to the operator as parameters or in other words operands. We have then called the getvalue method of the obj3 to show that it actually contains the sum of num variables of obj1 and obj2. The output Example1 is as follows.
In Example1, we overloaded our operator to add two object of a custom type ClassA. What if we want to add a custom type with a primitive type? For instance what would we do if we want to add an object of ClassA with an integer? The solution is identical what we did when we add two custom types with exception that now we will replace one of the objects that have been passes as parameter with an integer. In Example2, we have modified Example1 to explain this concept. Pay attention to that.
Inside the main function we added obj1 and the some integer and stored the resultant value in obj2 as we did in the Example1, we then called the getvalue function and displayed the value of num variable of object three which now contains the sum of obj1 and some integer which Is 30 in this case. The result will be 40 which has been displayed in the output.
Next come to this line
We know that for primitive types the above line equals obj3 = obj3 + 30; we know that num variable of obj3 now contains value 40. We are adding 30 to it. Now the result of this expression would be 70 which have been displayed in the second line of the output.
Using this technique, you can overload operators to perform arithmetic operations on all types of primitive as well as custom data types.
Operator overloading is not limited to arithmetic or logical operators, using overloaded implicit and explicit operators we can convert one data type to another. For Example, we can overload the aforementioned operators for converting integer type to a custom type ClassA which we have studied in Example1 and Example2, and vice versa. We will see with the help of an Example that how we can implement custom type conversion logic. Have a look at Example3.
In the above line we are explicitly casting integer into an object of ClassA, when the above line of code executes, the method we discussed above will be called. Inside the method, a new object of type ClassA will be created which will be passed the value of integer variable that has been passed as parameter to operator function and the object will be returned back to calling function. This was the explicit conversion.
Now come towards our second method.
The output of the Example3 is as follows:
From the above two methods we can infer following general rules while perform conversions using overloaded operators.
Operator overloading is a very vast and complex subject, which requires continuous practice and focus to be comprehended thoroughly. I would advise you to go through the above example more than once and try to solve complex overloading examples. For example, I would advise you to try conversion between object of two different types. That is an interesting problem and will widen your horizon and help you understand the concept more effectively. In case you face any problem, leave a reply and I will be back to help solve it for you!
|All times are GMT +5.5. The time now is 11:23.|