Overload comparison operator - CSharp Custom Type

CSharp examples for Custom Type:Operator Overloading

Description

Overload comparison operator

Demo Code

using System;//from ww w .j av a 2 s .c  o  m
class Pool {
   private double length;    // Length of a box
   private double width;    // Breadth of a box
   private double height;    // Height of a box
   public double getVolume() {
      return length * width * height;
   }
   public void setLength( double len ) {
      length = len;
   }
   public void setBreadth( double bre ) {
      width = bre;
   }
   public void setHeight( double hei ) {
      height = hei;
   }
   public static Pool operator+ (Pool b, Pool c) {
      Pool box = new Pool();
      box.length = b.length + c.length;
      box.width = b.width + c.width;
      box.height = b.height + c.height;
      return box;
   }
   public static bool operator == (Pool lhs, Pool rhs) {
      bool status = false;
      if (lhs.length == rhs.length && lhs.height == rhs.height && lhs.width == rhs.width) {
         status = true;
      }
      return status;
   }
   public static bool operator !=(Pool lhs, Pool rhs) {
      bool status = false;
      if (lhs.length != rhs.length || lhs.height != rhs.height || lhs.width != rhs.width) {
         status = true;
      }
      return status;
   }
   public static bool operator <(Pool lhs, Pool rhs) {
      bool status = false;
      if (lhs.length < rhs.length && lhs.height < rhs.height && lhs.width < rhs.width) {
         status = true;
      }
      return status;
   }
   public static bool operator >(Pool lhs, Pool rhs) {
      bool status = false;
      if (lhs.length > rhs.length && lhs.height > rhs.height && lhs.width > rhs.width) {
         status = true;
      }
      return status;
   }
   public static bool operator <=(Pool lhs, Pool rhs) {
      bool status = false;
      if (lhs.length <= rhs.length && lhs.height <= rhs.height && lhs.width <= rhs.width) {
         status = true;
      }
      return status;
   }
   public static bool operator >=(Pool lhs, Pool rhs) {
      bool status = false;
      if (lhs.length >= rhs.length && lhs.height >= rhs.height && lhs.width >= rhs.width) {
         status = true;
      }
      return status;
   }
   public override string ToString() {
      return String.Format("({0}, {1}, {2})", length, width, height);
   }
}
class Tester {
   static void Main(string[] args) {
      Pool Pool1 = new Pool();   // Declare Pool1 of type Pool
      Pool Pool2 = new Pool();   // Declare Pool2 of type Pool
      Pool Pool3 = new Pool();   // Declare Pool3 of type Pool
      Pool Pool4 = new Pool();
      double volume = 0.0;    // Store the volume of a box here
      Pool1.setLength(6.0);
      Pool1.setBreadth(7.0);
      Pool1.setHeight(5.0);
      // box 2 specification
      Pool2.setLength(12.0);
      Pool2.setBreadth(13.0);
      Pool2.setHeight(10.0);
      //displaying the Pooles using the overloaded ToString():
      Console.WriteLine("Pool 1: {0}", Pool1.ToString());
      Console.WriteLine("Pool 2: {0}", Pool2.ToString());
      // volume of box 1
      volume = Pool1.getVolume();
      Console.WriteLine("Volume of Pool1 : {0}", volume);
      // volume of box 2
      volume = Pool2.getVolume();
      Console.WriteLine("Volume of Pool2 : {0}", volume);
      // Add two object as follows:
      Pool3 = Pool1 + Pool2;
      Console.WriteLine("Pool 3: {0}", Pool3.ToString());
      // volume of box 3
      volume = Pool3.getVolume();
      Console.WriteLine("Volume of Pool3 : {0}", volume);
      //comparing the boxes
      if (Pool1 > Pool2)
         Console.WriteLine("Pool1 is greater than Pool2");
      else
         Console.WriteLine("Pool1 is  greater than Pool2");
      if (Pool1 < Pool2)
         Console.WriteLine("Pool1 is less than Pool2");
      else
          Console.WriteLine("Pool1 is not less than Pool2");
      if (Pool1 >= Pool2)
          Console.WriteLine("Pool1 is greater or equal to Pool2");
      else
          Console.WriteLine("Pool1 is not greater or equal to Pool2");
      if (Pool1 <= Pool2)
          Console.WriteLine("Pool1 is less or equal to Pool2");
      else
          Console.WriteLine("Pool1 is not less or equal to Pool2");
      if (Pool1 != Pool2)
          Console.WriteLine("Pool1 is not equal to Pool2");
      else
          Console.WriteLine("Pool1 is not greater or equal to Pool2");
      Pool4 = Pool3;
      if (Pool3 == Pool4)
         Console.WriteLine("Pool3 is equal to Pool4");
      else
         Console.WriteLine("Pool3 is not equal to Pool4");
  }
}

Related Tutorials