C# Operator Overloading
Overloading is generally defined as a process of implementing popular
Object-oriented programming concepts such as Polymorphism, which means one name having different forms and implementations. It allows the variables or the object to take different kinds of forms while executing the code. It is mainly used when you want the method property not to be similar to the given arguments, rather want a different order of execution when one name can be used with different types of execution methods and properties. This can be achieved in a program with different types of parameters and their count. There are various types of operator overloading methods in C#. You will learn all such methods in this tutorial conveniently.
Operator Overloading Techniques in C#
Operator overloading in C# can be done using different forms of operators. But before proceeding to the techniques, let's consider the validations of operators and how are they used while performing operator overloading.
The operator is usually is a keyword that is used while implementing operator overloading. It is important to note that the return type of the overloaded operator cannot be kept void. It means that while performing operator overloading, the preference is always in the over-defined implementations' hands. There is no preference given to predefined implementations.
Therefore, the overloaded methods should always have a different set of arguments. Their order and number should not be the same. They are similar to any other method, but in the case of user-defined methods, the operator's precedence and syntax cannot be changed. The left one is called a member in binary operators, while the right is an object called a parameter. You will understand these concepts once you go through the implementation for the same.
Another important instance to consider is that in C#, there's a special function known as the operator function. This function or method must always be static and public. It consists of only value arguments. The outer and the reference parameters are usually not allowed as arguments in operator functions. The general form of applying an operator function follows the following syntax.
Here, the op is the operator used to overload, and the operator is the only needed keyword. In the case of a unary operator, you would pass only one argument and two-argument for the binary operator. It is also important to note that atleast one argument should be user-defined or a struct type or a class. Let's see how to implement all these concepts.
Unary Operator Overloading
The general forms of operator overloading in the case of unary operator follows the syntax given below.
Here, the return type can be anything, but it should not be void for the operators like +,-, ~ and .(dot). The return type of this operator should be either an integer or a Boolean type. It is important to note that the Boolean operators roll out true and false and hence can be overloaded only as pairs. A compilation error would occur if not done because a class generally declares this operator without declaring the other.
Consider the following program that displays how unary operators inside a class Complex can be overloaded.
Binary Operator Overloading
To overload a binary operator, you must seek two arguments. You need to ensure that one of the operators should be of type class or struct where the operator is defined. As previously discussed, a binary operator cannot return void, but it can return all other types of values while implementing overloading. The general form of operator overloading in binary operators can be shown using the following syntax.
Consider the following example code that shows how binary operator overloading works.
It is important to keep a note that operators such as ==. !=, <>, <=, >= are only overloaded as pairs. When a binary arithmetic operator is overloaded using them, the assignment operators will automatically get overloaded. For instance, if you overload the + operator, it gets implicitly overloaded as += operator too.
Operator overloading & Inheritance
Another instance is that even if you declare the overloaded operators as static, they will inherit the derived class. This happens because the operator's declaration requires it to be struct or class where the operator is being declared. It facilitates the operator's signature. Thus, it is not possible for an operator previously declared in the derived class to hide an operator present in the parent class already. Hence, the new modifier is not an option to consider since they are not allowed while declaring the operator. This instance can be shown using the following code snippet given below.
Equality Operator Overloading
You might have already been familiar with all the user-defined classes that inherit Syste.object from the Syste.object.Equals() method by default, in C#. The reference-based comparison is provided by the Equals() method. But there is a possibility that this method can override the methods residing inside the class defined by the user. Hence, value-based comparison can be easily achieved by using this method. This is how the Equality operator is operated. To overload this operator, you need to follow the following code snippet shown below.
In the above program, the results are displayed as "NOT OJ" and "OK1". This usually means that by default, the Equals() method is performing a referential comparison. It is important to note that the value of the objects C2 and C1 are the same in the above code, but they have different preferences in the memory address. But if you carefully note, the C2 and C3 share the same memory reference. This is how the Equals() method implements equality operator overloading. In C#, it is also possible to override the Equals() method even though it might be present in any class defined by the user to achieve value-based comparison. This statement can be supported using the following code snippet shown below.
In this tutorial, you learned about the overall methods that are generally used to implement operator overloading in C#. You came across certain instances where operator overloading is implemented using unary, binary and equality operators. You might also have been familiar by now that various operators while implementing C# restrict the flow of program hence you need to understand where they outlay those conditions. Also, while implementing operator overloading in C#, you need to be very curious to understand what is the workflow of the data being rolled out while implementation and how one form of data can deliver different kinds of outputs.