Overload > and < : Relotional Operator Overload « Operator Overload « C# / CSharp Tutorial






using System;  
  
class TwoDimension {  
  int x, y;
  
  public TwoDimension() { 
     x = y = 0; 
  }  
  public TwoDimension(int i, int j) { 
     x = i; 
     y = j;
  }  
  
  // Overload <.  
  public static bool operator <(TwoDimension op1, TwoDimension op2)  
  {  
    if((op1.x < op2.x) && (op1.y < op2.y))  
      return true;  
    else  
      return false;  
  }  
  
  // Overload >.  
  public static bool operator >(TwoDimension op1, TwoDimension op2)  
  {  
    if((op1.x > op2.x) && (op1.y > op2.y))  
      return true;  
    else  
      return false;  
  }  
  
  // Show X, Y
  public void show()  
  {  
    Console.WriteLine(x + ", " + y);  
  }  
}  
  
class TwoDimensionDemo {  
  public static void Main() {  
    TwoDimension a = new TwoDimension(5, 6);  
    TwoDimension b = new TwoDimension(10, 10);  
    TwoDimension c = new TwoDimension(1, 2);  
  
    Console.Write("Here is a: ");  
    a.show();  
    Console.Write("Here is b: ");  
    b.show();  
    Console.Write("Here is c: ");  
    c.show();  
    Console.WriteLine();  
  
    if(a > c) Console.WriteLine("a > c is true");  
    if(a < c) Console.WriteLine("a < c is true");  
    if(a > b) Console.WriteLine("a > b is true");  
    if(a < b) Console.WriteLine("a < b is true");  
  }  
}
Here is a: 5, 6
Here is b: 10, 10
Here is c: 1, 2

a > c is true
a < b is true








8.3.Relotional Operator Overload
8.3.1.Overload > and <
8.3.2.Overloading Relational Operators