A Beginner’s Tutorial on Operator Overloading in C#

This article talks about the operator overloading in C#. What are the various types of operators that can be overloaded. We will create a small dummy application to see how can we overload some basic operators.

Background

In an object oriented programming language like C#, operator overloading provides a much more natural way of implementing the operations on custom types. Suppose that we have a class created for Complex number and we want to perform all the arithmetic operations on this type. One way to do this is by having functions like Add, Subtract inside the class and have the functionality. Another way is to actually have the overloaded version of operators to act on this type.

Operator overloading provides a much natural abstraction for the types. When we think about possible operation on some data type we can think of binary operators, unary operators, relational operators and perhaps some conversion operations to and from the basic types. In C# achieving all this is possible using operator overloading.

Using the code

Before looking at the implementation details, lets see what are the conventions that need to be followed if we want to overload an operator.

  • The operator function should be a member function of the containing type.
  • The operator function must be static.
  • The operator function must have the keyword operator followed by the operator to be overridden.
  • The arguments of the function are the operands.
  • The return value of the function is the result of the operation.

So to understand the above mentioned points more clearly we can visualize the operators getting invoked as function calls (below code will not compile, it is only for understanding purpose).

So following the above guidelines, lets us see how we can implement operator overloading. We will create a small class Rationalrepresenting a rational number and will implement some basic overloaded operators in it.

Overloading the Binary operators

Binary operators function will take 2 arguments and return a new object of the Containing type. Let us try to implement the binary operator + for our Rationalclass.

Once we have the binary operator overloaded we can use it simply as

The important thing to note here is that once we have the binary operator + implemented the compound assignment operator for that operator i.e. += in this case also gets implemented in terms of this operator i.e. + in this case.

The other possibility could be that we may want to have mix mode arithmetic with our type i.e. we may want to have the arithmetic operations to work with our Rationaltype and some primitive type. So if we want to have the possibility of having Rational + int then we will have to implement an overloaded function for that too.

Overloading the Unary operators

To overload the unary operators, we need a function taking only one argument of the containing type. The important thing in case of overloading unary operators is that we should not create a new object but instead change the value of the object being passed to us and return it. Let us implement the unary ++ for our Rational type now.

The thing to notice in the above code snippet is that we overloaded ++ and C# internally took care of using it in pre-increment and post-increment scenario. So unlike C++ we need not implement the separate versions for prefix and post-fix unary operators.

Overloading the Relational operators

Relational operators like < and > can also be overloaded as functions taking two arguments. The important thing to note is that we need to overload relational operators in pairs i.e. if I am overloading < operator than I will have to overload > operator too. Same is true for (<=, >=)and (==, !=)operators. So let us go ahead and implement the relational operators in our Rationalclass.

Other important thing to note is while implementing the equality i.e. == operator. If we are overloading == operator then we need to implement != operator too(as discussed above). Also, we need to override the Equalsand GetHashCodefunctions so that if our object returns true for == then it should return truefor Equals function too and should return the same value from GetHashCode().

Overloading the Conversion operators

We might also need to implement conversion operators sometimes so that our type can safely be converted to and from other types. We can define the conversion operators as implicitor explicit. In case of implicitconversion the user will not have to explicitly type cast our type to target type and our conversion operation will work. In case of explicitconversion the user will have to explicitly cast our type to target type to invoke our conversion operation. If the casting is not performed it will give a compile time error.

Let us go ahead and define the conversion operation for our Rational type. We will implicitly convert integertypes to Rationaltype but we will keep the conversion from Rationalto doubleexplicit.

Now We have some basic operators overloaded/implemented for this Rationalclass. We have implemented some of the operators in each category of operators, rest of the operators can be overloaded on same lines.

Note: Please refer to the attached sample code to see the complete class with sample test code.

Point of interest

This article is written as a walk-through tutorial on operator overloading in C# from a beginner’s perspective. Most of the veteran programmers are already aware of this basic stuff and find the information mundane, Still I hope this has been informative.

Download sample code for this article: OperatorOverloadingDemo