Operator Overloading in C#

Introduction

In this article we will learn operator overloading and the basic concepts of operator overloading. In the contrast operator overloading works on the operator, where we can expand its usages to our class and effects of the operator. It comes under our control and it might differ from class to class. Microsoft includes operator overloading for C# in 2001.

Fundamental of Operator Overloading

Operator overloading is the ability to use the same operator (like arithmetic operator , +, - , * , /) to do various operations. The operator keywords declares a function specifying what the operator- symbol means when applied to an instance of a class.  Operator overloading gives us the ability to user-define implementations of operations where one or both operands are of a user-defined class. This usually helps to work with objects (any class) as a variable with primitive data type. It is used in semantic convenience, readability and maintainability.    

Operator overloading is closely related to method overloading. For overloading an operator, we use the operator keyword to define a operator method, that defines the action of the operator relative to its class.

There are two basic forms of the operator method:  

  • Unary operator
  • Binary operator

Syntax

Unary Operator syntax

  1. public static return-type operator op(param-type operand)  
  2. {  
  3.      // operations  
  4. }  
Binary Operator syntax   
  1. public static return-type operator op(param-type1 operand1,param-type1 operand2)  
  2. {    
  3.      // operations    
  4. }  
  • op:  Operator for overloading
  • return-type:  As the name suggested , this is the return type and it might be any type, whatever we choose,
                             the return-type value should be the same type as the class of the operator being overloaded.
  • param-type:  It is a parameter value that we are passing through the method, using an operand.

NOTE: The operator parameter must not be a ref or out modifier.

  1. using System;    
  2. namespace OperatorOverloading    
  3. {    
  4.     class Program    
  5.     {    
  6.         int x, y, z;    
  7.         public Program()    
  8.         {    
  9.             x = y = z = 0;    
  10.         }    
  11.         public Program(int i, int j, int k)    
  12.         {    
  13.             x = i;    
  14.             y = j;    
  15.             z = k;    
  16.         }    
  17.         public static Program operator +(Program obj1, Program oj2)    
  18.         {    
  19.             Program result = new Program();    
  20.             result.x = obj1.x + oj2.x;    
  21.             result.y = obj1.y + oj2.y;    
  22.             result.z = obj1.z + oj2.z;    
  23.             return result;    
  24.         }    
  25.         public static Program operator -(Program obj1, Program oj2)    
  26.         {    
  27.             Program result = new Program();    
  28.             result.x = obj1.x - oj2.x;    
  29.             result.y = obj1.y - oj2.y;    
  30.             result.z = obj1.z - oj2.z;    
  31.             return result;    
  32.         }    
  33.         public void show()    
  34.         {    
  35.             Console.WriteLine(x + "," + y + "," + z);    
  36.         }    
  37.         static void Main(string[] args)    
  38.         {    
  39.             Program objA = new Program(1, 2, 3);    
  40.             Program objB = new Program(25, 22, 10);    
  41.             Program objC;    
  42.             Console.WriteLine("The value of objA: ");    
  43.             objA.show();    
  44.             Console.WriteLine();    
  45.     
  46.             Console.WriteLine("The value of objB: ");    
  47.             objB.show();    
  48.             Console.WriteLine();    
  49.                 
  50.             objC = objA + objB;    
  51.             Console.WriteLine("Result of objA + objB");    
  52.             objC.show();    
  53.             Console.WriteLine();    
  54.                 
  55.             objC = objA + objB + objC;    
  56.             Console.WriteLine("adding objA, onjB and objC");    
  57.             objC.show();    
  58.             Console.WriteLine();    
  59.                 
  60.             objCobjC = objC - objA;    
  61.             Console.WriteLine("Subtracting objC - objA");    
  62.             objC.show();    
  63.             Console.WriteLine();    
  64.     
  65.             objCobjC = objC - objB;    
  66.             Console.WriteLine("Subtracting obhC - objB");    
  67.             objC.show();    
  68.             Console.WriteLine();    
  69.             Console.Read();    
  70.     
  71.         }    
  72.     }    
  73. }    

 BinaryOverloading

 Summary

In this article we learned operator overloading; the basic definition and why we use operator overloading in C# programs, what are the significance of operator overloading and the various types of operator overloading.