Operator Overloading In C#

What is operator?

Operators are the symbols, which are used to perform an operation between the operands. As,
 
a + b 
Operands : a and b
Operator : +
 
Type of operators:
 
Operators are of two types, which are:
 
Unary Operators : ++, --, -
 
Binary Operators : +, -, *, /, >, <, =, == etc.
 
Why operator overloading is required? 
 
Usually, the operators work with the pre-defind type like int, float, string etc. If you want to perform an operation on the user defined type (object), it is required to re-define the operators (overload) to perform the operation.  
  1. Number n1=new Number(10,20);  
  2. Number n2=new Number(5,10);  
  3. Number n3=n1+n2;   
n1 and n2 are two user defined types (object) and + operator is required to perform an addition between the two objects. This is only possible with the operator overloading.
 
Example:
 
Create a class to define a user defined type (object). 
  1.  class Numbers  
  2.  {  
  3.         public int num1, num2;  
  4.         public Numbers()  
  5.         {  
  6.             num1 = 0;  
  7.             num2 = 0;  
  8.         }  
  9.         public Numbers(int a,int b)  
  10.         {  
  11.             num1 = a;  
  12.             num2 = b;  
  13.         }  
  14.         public void PrintNumbers()  
  15.         {  
  16.             Console.WriteLine("Num1 :"+num1);  
  17.             Console.WriteLine("Num2 :" + num2);  
  18.         }  
  19. }    
Write the code to redefine the operators (overload) to perform a user defined operation.
 
Operator overloading : + 
  1. public static Numbers operator +(Numbers ob1, Numbers ob2)  
  2. {  
  3.             Numbers ob3 = new Numbers();  
  4.             ob3.num1 = ob1.num1 + ob2.num1;  
  5.             ob3.num2 = ob1.num2 + ob2.num2;  
  6.             return ob3;  
  7. }   
Operator overloading : ++ 
  1. public static Numbers operator ++(Numbers ob1)  
  2. {  
  3.             Numbers ob2 = new Numbers ();  
  4.             ob2.num1 = ob1.num1 + 1;  
  5.             ob2.num2 = ob1.num2 + 1;  
  6.             return ob2;  
  7. }    
Operator overloading : > 
  1. public static bool operator >(Numbers ob1, Numbers ob2)  
  2.         {  
  3.             if (ob1.num1 > ob2.num1 && ob1.num2 > ob2.num2)  
  4.             {  
  5.                 return true;  
  6.             }  
  7.             else  
  8.             {  
  9.                 return false;  
  10.             }  
  11. }   
Operator overloading : <
  1. public static bool operator <(Numbers ob1, Numbers ob2)  
  2. {  
  3.             if (ob1.num1 < ob2.num1 && ob1.num2 < ob2.num2)  
  4.             {  
  5.                 return true;  
  6.             }  
  7.             else  
  8.             {  
  9.                 return false;  
  10.             }  
  11. }   
Write the statement in Main method to test the Application.
 
Statement 1 - To test + operator overloading.
  1. Numbers n1 = new Numbers();  
  2. Numbers n2 = new Numbers(10,20);  
  3. Numbers n3 = new Numbers(20,30);  
  4. n1 = n2 + n3;  
  5. n1.PrintNumbers();   
Statement 2 - To test ++ operator overloading.
  1. Numbers n1 = new Numbers();  
  2. Numbers n2 = new Numbers(10,20);  
  3. //Pre increment  
  4. n1 = ++n2;  
  5. n1.PrintNumbers();  
  6. n2.PrintNumbers();   
  7.   
  8. //Post increment  
  9. n1 = n2++;  
  10. n1.PrintNumbers();  
  11. n2.PrintNumbers();    
Statement 3 - To test >operator overloading.
  1. Numbers n1 = new Numbers(10,20);  
  2. Numbers n2 = new Numbers(20,30);   
  3. if (n1 > n2)  
  4. {  
  5.     Console.WriteLine("True");  
  6. }  
  7. else  
  8. {  
  9.     Console.WriteLine("False");  
  10.  
Statement 4 - To test < operator overloading. 
  1. Numbers n1 = new Numbers(10,20);  
  2. Numbers n2 = new Numbers(20,30);   
  3. if (n1 < n2)  
  4. {  
  5.     Console.WriteLine("True");  
  6. }  
  7. else  
  8. {  
  9.     Console.WriteLine("False");  
  10. }   
Note

Some of the operators are not overloaded. As- =, ?:,->, sizeof, new etc.The comparison operators must be overloaded in the pairs.
 
That's all. Thank you. If any suggestion or clarification is required, kindly revert back with the comments.
Next Recommended Reading Main advantage of Operator overloading