Operator Overloading

Explain operator overloading.

All unary and binary operators have pre-defined implementations, that are automatically available in any expressions. In addition to this pre-defined implementations, user defined implementations can also be introduced in C#. The mechanism of giving a special meaning to a standard C# operator with respect to a user defined data type such as classes or structures is known as operator overloading. Remember that it is not possible to overload all operators in C#. The following table shows the operators and their overloadability in C#.

 

Operators

Overloadability 

+, -, *, /, %, &, |, <<, >>

All C# binary operators can be overloaded.

+, -, !,  ~, ++, --, true, false

All C# unary operators can be overloaded.

==, !=, <, >, <= , >=

All relational operators can be overloaded, but only as pairs.

&&, ||

They can't be overloaded.

[] (Array index operator)

They can't be overloaded.

() (Conversion operator)

They can't be overloaded.

+=, -=, *=, /=, %=

These compound assignment operators can be overloaded. But in C#, these operators are automatically overloaded when the respective binary operator is overloaded.

=, . , ?:, ->, new, is, as, sizeof

These operators can't be overloaded in C#.

 

In C#, a special function called operator function is used for overloading purpose. These special function or method must be public and static. They can take only value arguments. The ref and out parameters are not allowed as arguments to operator functions. The general form of an operator function is as follows.

  1. public static return_type operator op (argument list)  

Where the op is the operator to be overloaded and operator is the required keyword. For overloading the unary operators, there is only one argument and for overloading a binary operator there are two arguments. Remember that at least one of the arguments must be a user-defined type such as class or struct type.

 

The following program overloads the unary - operator inside the class Complex

  1. // Unary operator overloading  
  2. using System;  
  3. class Complex  
  4. {  
  5.     private int x;  
  6.     private int y;  
  7.     public Complex()  
  8.     {  
  9.     }  
  10.     public Complex(int i, int j)  
  11.     {  
  12.         x = i;  
  13.         y = j;  
  14.     }  
  15.     public void ShowXY()  
  16.     {  
  17.         Console.WriteLine("{0} {1}", x, y);  
  18.     }  
  19.     public static Complex operator -(Complex c)  
  20.     {  
  21.         Complex temp = new Complex();  
  22.         temp.x = -c.x;  
  23.         temp.y = -c.y;  
  24.         return temp;  
  25.     }  
  26. }  
  27. class MyClient  
  28. {  
  29.     public static void Main()  
  30.     {  
  31.         Complex c1 = new Complex(10, 20);  
  32.         c1.ShowXY(); // displays 10 & 20  
  33.         Complex c2 = new Complex();  
  34.         c2.ShowXY(); // displays 0 & 0  
  35.         c2 = -c1;  
  36.         c2.ShowXY(); // diapls -10 & -20  
  37.     }  
  38. }  

Overloading Binary Operators

An overloaded binary operator must take two arguments; at least one of them must be of the type class or struct, in which the operation is defined. But overloaded binary operators can return any value except the type void. The general form of a overloaded binary operator is as follows.

  1. public static return_type operator op (Type1 t1, Type2 t2)  
  2. {  
  3. //Statements  
  4. }  

A concrete example is given below.

  1. // Binary operator overloading  
  2. using System;  
  3. class Complex  
  4. {  
  5.     private int x;  
  6.     private int y;  
  7.     public Complex()  
  8. }  
  9. public Complex(int i, int j)  
  10. {  
  11.     x = i;  
  12.     y = j;  
  13. }  
  14. public void ShowXY()  
  15. {  
  16.     Console.WriteLine("{0} {1}", x, y);  
  17. }  
  18. public static Complex operator +(Complex c1, Complex c2)  
  19. {  
  20.     Complex temp = new Complex();  
  21.     temp.x = c1.x + c2.x;  
  22.     temp.y = c1.y + c2.y;  
  23.     return temp;  
  24. }  
  25. }  
  26. class MyClient  
  27. {  
  28.     public static void Main()  
  29.     {  
  30.         Complex c1 = new Complex(10, 20);  
  31.         c1.ShowXY(); // displays 10 & 20  
  32.         Complex c2 = new Complex(20, 30);  
  33.         c2.ShowXY(); // displays 20 & 30  
  34.         Complex c3 = new Complex();  
  35.         c3 = c1 + c2;  
  36.         c3.ShowXY(); // dislplays 30 & 50  
  37.     }  
  38. }  

The binary operators such as = =, ! =, <, >, < =, > = can be overloaded only as pairs. Remember that when a binary arithmetic operator is overloaded, corresponding assignment operators also get overloaded automatically. For example if we overload + operator, it implicitly overloads the + = operator also.

Leave Comment

Important Topics

Title
Object Oriented Programming (OOP)
OOP vs POP
Class with Example
Objects
Access Modifiers
Encapsulation with example
C# Properties (GET, SET)
Method and Function
Abstraction
Polymorphism
Operator Overloading
Inheritance
Constructor and its Types
Exception Handling
Throwing an Exception