A better way to overload !, |, and & for TwoDimension. : operator overload « Operator Overload « C# / CSharp Tutorial






This version automatically enables the && and || operators.

using System;   
   
class TwoDimension {   
  int x, y;
   
  public TwoDimension() { 
     x = y = 0; 
  }   
  public TwoDimension(int i, int j) { 
     x = i; 
     y = j;
  }   
 
 
  // Overload | for short-circuit evaluation.   
  public static TwoDimension operator |(TwoDimension op1, TwoDimension op2)   
  {  
    if( ((op1.x != 0) || (op1.y != 0) ) | 
       ((op2.x != 0) || (op2.y != 0)) ) 
      return new TwoDimension(1, 1);   
    else   
      return new TwoDimension(0, 0);   
  }   
 
  // Overload & for short-circuit evaluation.   
  public static TwoDimension operator &(TwoDimension op1, TwoDimension op2)   
  {   
    if( ((op1.x != 0) && (op1.y != 0)) & 
       ((op2.x != 0) && (op2.y != 0) ) ) 
      return new TwoDimension(1, 1);   
    else   
      return new TwoDimension(0, 0);   
  }   
 
  // Overload !.   
  public static bool operator !(TwoDimension op)   
  {   
    if(op) return false;   
    else return true;   
  }   
 
  // Overload true.   
  public static bool operator true(TwoDimension op) { 
    if((op.x != 0) || (op.y != 0)) 
      return true; // at least one coordinate is non-zero 
    else 
      return false; 
  }   
 
  // Overload false. 
  public static bool operator false(TwoDimension op) { 
    if((op.x == 0) && (op.y == 0)) 
      return true; // all coordinates are zero 
    else 
      return false; 
  }   
 
  // Show X, Y
  public void show()   
  {   
    Console.WriteLine(x + ", " + y);   
  }   
}   
   
class MainClass {   
  public static void Main() {   
    TwoDimension a = new TwoDimension(5, 6);   
    TwoDimension b = new TwoDimension(10, 10);   
    TwoDimension c = new TwoDimension(0, 0);   
   
    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) {
       Console.WriteLine("a is true."); 
    }
    
    if(b) {
        Console.WriteLine("b is true."); 
    }
    
    if(c) {
        Console.WriteLine("c is true."); 
    }
    
 
    if(!a) {
       Console.WriteLine("a is false."); 
    }
    if(!b) {
       Console.WriteLine("b is false."); 
    }
    if(!c) {
       Console.WriteLine("c is false."); 
    }
 
    Console.WriteLine(); 
 
    Console.WriteLine("Use & and |"); 
    if(a & b) 
       Console.WriteLine("a & b is true."); 
    else 
       Console.WriteLine("a & b is false."); 
 
    if(a & c) 
       Console.WriteLine("a & c is true."); 
    else 
       Console.WriteLine("a & c is false."); 
 
    if(a | b) 
       Console.WriteLine("a | b is true."); 
    else 
       Console.WriteLine("a | b is false."); 
 
    if(a | c) 
       Console.WriteLine("a | c is true."); 
    else 
       Console.WriteLine("a | c is false."); 
 
    Console.WriteLine(); 
 
    // now use short-circuit ops 
    Console.WriteLine("Use short-circuit && and ||"); 
    if(a && b) 
        Console.WriteLine("a && b is true."); 
    else 
        Console.WriteLine("a && b is false."); 
 
    if(a && c) 
        Console.WriteLine("a && c is true."); 
    else 
        Console.WriteLine("a && c is false."); 
 
    if(a || b) 
        Console.WriteLine("a || b is true."); 
    else 
        Console.WriteLine("a || b is false."); 
 
    if(a || c) 
        Console.WriteLine("a || c is true."); 
    else 
        Console.WriteLine("a || c is false."); 
  }   
}
Here is a: 5, 6
Here is b: 10, 10
Here is c: 0, 0

a is true.
b is true.
c is false.

Use & and |
a & b is true.
a & c is false.
a | b is true.
a | c is true.

Use short-circuit && and ||
a && b is true.
a && c is false.
a || b is true.
a || c is true.








8.1.operator overload
8.1.1.Valid Overloadable Operators
8.1.2.Operator Overloading: A Complex Number Class
8.1.3.Operator Overloading for your own class
8.1.4.A better way to overload !, |, and & for TwoDimension.