Latest web development tutorials

C # operator overloading

You can redefine or override the built-in C # operator. Therefore, programmers can use the user-defined type operators. Overloaded operators are having a special function name, followed by the keywordoperator operator symbol definition.And other functions, overloaded operators have a return type and parameter list.

For example, consider 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 is user-defined class Box implements the addition operator (+). It is the property of two Box objects are added, and the added return Box objects.

Operator overloading to achieve

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;
      }
      // Overloaded + operator to the sum of the 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 Box1, type Box
         Box Box2 = new Box (); // declare Box2, type Box
         Box Box3 = new Box (); // declare Box3, type Box
         double volume = 0.0; // // Box1 volume detailing Box1.setLength (6.0);
         Box1.setBreadth (7.0);
         Box1.setHeight (5.0);

         // Box2 detailing Box2.setLength (12.0);
         Box2.setBreadth (13.0);
         Box2.setHeight (10.0);

         // Box1 volume of volume = Box1.getVolume ();
         Console.WriteLine ( "Box1 volume: {0}", volume);

         // Box2 volume of volume = Box2.getVolume ();
         Console.WriteLine ( "Box2 volume: {0}", volume);

         // The two objects are added Box3 = Box1 + Box2;

         // Box3 volume of volume = Box3.getVolume ();
         Console.WriteLine ( "Box3 volume: {0}", volume);
         Console.ReadKey ();
      }
   }
}

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

Box1 volume: 210
Box2 volume: 1560
Box3 volume: 5400

Reloadable and non-overloaded operators

The following table describes the ability of the C # operator overloading:

运算符描述
+, -, !, ~, ++, --这些一元运算符只有一个操作数,且可以被重载。
+, -, *, /, %这些二元运算符带有两个操作数,且可以被重载。
==, !=, <, >, <=, >=这些比较运算符可以被重载。
&&, ||这些条件逻辑运算符不能被直接重载。
+=, -=, *=, /=, %=这些赋值运算符不能被重载。
=, ., ?:, ->, new, is, sizeof, typeof这些运算符不能被重载。

Examples

For the above discussion, let's extend the example above, more overloaded 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;
      }
      // Overloaded + operator to the sum of the 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})", length, breadth, height);
      }
   
   }
    
   class Tester
   {
      static void Main (string [] args)
      {
        Box Box1 = new Box (); // declare Box1, type Box
        Box Box2 = new Box (); // declare Box2, type Box
        Box Box3 = new Box (); // declare Box3, type Box
        Box Box4 = new Box ();
        double volume = 0.0; // // Box1 volume detailing Box1.setLength (6.0);
        Box1.setBreadth (7.0);
        Box1.setHeight (5.0);

        // Box2 detailing Box2.setLength (12.0);
        Box2.setBreadth (13.0);
        Box2.setHeight (10.0);

       // Use the overloaded ToString () displays two boxes Console.WriteLine ( "Box1: {0}", Box1.ToString ());
        Console.WriteLine ( "Box2: {0}", Box2.ToString ());
        
        // Box1 volume of volume = Box1.getVolume ();
        Console.WriteLine ( "Box1 volume: {0}", volume);

        // Box2 volume of volume = Box2.getVolume ();
        Console.WriteLine ( "Box2 volume: {0}", volume);

        // The two objects are added Box3 = Box1 + Box2;
        Console.WriteLine ( "Box3: {0}", Box3.ToString ());
        // Box3 volume of volume = Box3.getVolume ();
        Console.WriteLine ( "Box3 volume: {0}", volume);

        // Comparing the boxes
        if (Box1> Box2)
          Console.WriteLine ( "Box1 greater than Box2");
        else
          Console.WriteLine ( "Box1 not more than Box2");
        if (Box1 <Box2)
          Console.WriteLine ( "Box1 less than Box2");
        else
          Console.WriteLine ( "Box1 not less than Box2");
        if (Box1> = Box2)
          Console.WriteLine ( "Box1 greater than or equal Box2");
        else
          Console.WriteLine ( "Box1 not greater than or equal Box2");
        if (Box1 <= Box2)
          Console.WriteLine ( "Box1 less Box2");
        else
          Console.WriteLine ( "Box1 not less Box2");
        if (Box1! = Box2)
          Console.WriteLine ( "Box1 not equal Box2");
        else
          Console.WriteLine ( "Box1 equal Box2");
        Box4 = Box3;
        if (Box3 == Box4)
          Console.WriteLine ( "Box3 equal Box4");
        else
          Console.WriteLine ( "Box3 not equal Box4");

        Console.ReadKey ();
      }
    }
}

When the above code is compiled and executed, it produces 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 Box2 not more than
Box1 Box2 less than
Box1 Box2 is not greater than or equal
Box1 Box2 less
Box1 Box2 not equal
Box3 equal Box4