English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# Operator Overloading

You can redefine or overload built-in operators in C#. Therefore, programmers can also use operators of user-defined types. Overloading operators are functions with special names, accessed by the keyword operator defined by following the symbol of the operator. Like other functions, overloaded operators have a return type and a parameter list.

For example, see the following function:

public static Box operator+ (Box b, Box c)
{
   Box box = new Box();
   box.length = b.length + c.length;
   box.breadth = b.breadth + c.breadth;
   box.height = b.height + c.height;
   return box;
}

The above function implements the addition operator for the user-defined class Box (+)。It adds the properties of two Box objects and returns the Box object after addition.

Implementation of Operator Overloading

The following program demonstrates the complete implementation:

using System;
namespace OperatorOvlApplication
{
   class Box
   {
      private double length;      // length
      private double breadth;     // width
      private double height;      // height
      public double getVolume()
      {
         return length * breadth * height
      }
      public void setLength(double len)
      {
         length = len;
      }
      public void setBreadth(double bre)
      {
         breadth = bre;
      }
      public void setHeight(double hei)
      {
         height = hei;
      }
      // overload + operator to add two Box objects
      public static Box operator+ (Box b, Box c)
      {
         Box box = new Box();
         box.length = b.length + c.length;
         box.breadth = b.breadth + c.breadth;
         box.height = b.height + c.height;
         return box;
      }
   }
   class Tester
   {
      static void Main(string[] args)
      {
         Box Box1 = new Box();         // declare Box1of type Box
         Box Box2 = new Box();         // declare Box2of type Box
         Box Box3 = new Box();         // declare Box3of type Box
         double volume = 0.0;          // Volume
         // Box1 Details
         Box1.setLength(6;
         Box1.setBreadth(7;
         Box1.setHeight(5;
         // Box2 Details
         Box2.setLength(12;
         Box2.setBreadth(13;
         Box2.setHeight(10;
         // Box1 volume
         volume = Box1;
         Console.WriteLine("Box1 of volume: {0}
         // Box2 volume
         volume = Box2;
         Console.WriteLine("Box2 of volume: {0}
         // add two objects
         Box3 Equal to Box1 + Box2;
         // Box3 volume
         volume = Box3;
         Console.WriteLine("Box3 of volume: {0}
         Console.ReadKey();
      }
   }
}

When the above code is compiled and executed, it will produce the following results:

Box1 Volume: 210
Box2 Volume: 1560
Box3 Volume: 5400

Overloadable and Non-overloadable Operators

The following table describes the ability to overload operators in C#:

OperatorDescription
+, -, !, ~, ++, --These unary operators have only one operand and can be overloaded.
+, -, *, /, %These binary operators have two operands and can be overloaded.
==, !=, <, >, <=, >=These comparison operators can be overloaded.
&&, ||These conditional logical operators cannot be overloaded directly.
+=, -=, *=, /=, %=These assignment operators cannot be overloaded.
=, ., ?:, ->, new, is, sizeof, typeofThese operators cannot be overloaded.

Online example

In light of the above discussion, let us extend the example above by overloading more operators:

using System;
namespace OperatorOvlApplication
{
    class Box
    {
       private double length;      // length
       private double breadth;     // width
       private double height;      // height
      
       public double getVolume()
       {
         return length * breadth * height
       }
      public void setLength(double len)
      {
          length = len;
      }
      public void setBreadth(double bre)
      {
          breadth = bre;
      }
      public void setHeight(double hei)
      {
          height = hei;
      }
      // overload + operator to add two Box objects
      public static Box operator+ (Box b, Box c)
      {
          Box box = new Box();
          box.length = b.length + c.length;
          box.breadth = b.breadth + c.breadth;
          box.height = b.height + c.height;
          return box;
      }
      
      public static bool operator ==(Box lhs, Box rhs)
      {
          bool status = false;
          if (lhs.length == rhs.length && lhs.height == rhs.height 
             && lhs.breadth == rhs.breadth)
          {
              status = true;
          }
          return status;
      }
      public static bool operator !=(Box lhs, Box rhs)
      {
          bool status = false;
          if (lhs.length != rhs.length || lhs.height != rhs.height 
              || lhs.breadth != rhs.breadth)
          {
              status = true;
          }
          return status;
      }
      public static bool operator <(Box lhs, Box rhs)
      {
          bool status = false;
          if (lhs.length < rhs.length && lhs.height 
              < rhs.height && lhs.breadth < rhs.breadth)
          {
              status = true;
          }
          return status;
      }
      public static bool operator >(Box lhs, Box rhs)
      {
          bool status = false;
          if (lhs.length > rhs.length && lhs.height 
              > rhs.height && lhs.breadth > rhs.breadth)
          {
              status = true;
          }
          return status;
      }
      public static bool operator <=(Box lhs, Box rhs)
      {
          bool status = false;
          if (lhs.length <= rhs.length && lhs.height 
              <= rhs.height && lhs.breadth <= rhs.breadth)
          {
              status = true;
          }
          return status;
      }
      public static bool operator >=(Box lhs, Box rhs)
      {
          bool status = false;
          if (lhs.length >= rhs.length && lhs.height 
             >= rhs.height && lhs.breadth >= rhs.breadth)
          {
              status = true;
          }
          return status;
      }
      public override string ToString()
      {
          return String.Format("({0}, ",1}, {2}) {
      }
   
   }
    
   class Tester
   {
      static void Main(string[] args)
      {
        Box Box1 = new Box();          // declare Box1of type Box
        Box Box2 = new Box();          // declare Box2of type Box
        Box Box3 = new Box();          // declare Box3of type Box
        Box Box4 = new Box();
        double volume = 0.0;   // Volume
        // Box1 Details
        Box1.setLength(6;
        Box1.setBreadth(7;
        Box1.setHeight(5;
        // Box2 Details
        Box2.setLength(12;
        Box2.setBreadth(13;
        Box2.setHeight(10;
       // using overloaded ToString() to display two boxes
        Console.WriteLine("Box1: {0}1;
        Console.WriteLine("Box2: {0}2;
        
        // Box1 volume
        volume = Box1;
        Console.WriteLine("Box1 of volume: {0}
        // Box2 volume
        volume = Box2;
        Console.WriteLine("Box2 of volume: {0}
        // add two objects
        Box3 Equal to Box1 + Box2;
        Console.WriteLine("Box3: {0}3;
        // Box3 volume
        volume = Box3;
        Console.WriteLine("Box3 of volume: {0}
        //comparing the boxes
        If Box1 > Box2)
          Console.WriteLine("Box1 greater than Box2");
        else
          Console.WriteLine("Box1 Not Greater Than Box2");
        If Box1 < Box2)
          Console.WriteLine("Box1 Less Than Box2");
        else
          Console.WriteLine("Box1 Not Less Than Box2");
        If Box1 Greater Than or Equal to Box2)
          Console.WriteLine("Box1 Greater Than or Equal to Box2");
        else
          Console.WriteLine("Box1 Not Greater Than or Equal to Box2");
        If Box1 Less Than or Equal to Box2)
          Console.WriteLine("Box1 Less Than or Equal to Box2");
        else
          Console.WriteLine("Box1 Not Less Than or Equal to Box2");
        If Box1 Not Equal to Box2)
          Console.WriteLine("Box1 Not Equal to Box2");
        else
          Console.WriteLine("Box1 Equal to Box2");
        Box4 Equal to Box3;
        If Box3 Equal to Box4)
          Console.WriteLine("Box3 Equal to Box4");
        else
          Console.WriteLine("Box3 Not Equal to Box4");
        Console.ReadKey();
      }
    }
}

When the above code is compiled and executed, it will produce the following results:

Box1: (6, 7, 5)
Box2: (12, 13, 10)
Box1 Volume: 210
Box2 Volume: 1560
Box3: (18, 20, 15)
Box3 Volume: 5400
Box1 Not Greater Than Box2
Box1 Less Than Box2
Box1 Not Greater Than or Equal to Box2
Box1 Less Than or Equal to Box2
Box1 Not Equal to Box2
Box3 Equal to Box4