[Mono-devel-list] Comparing against null

Joe Ante joe at otee.dk
Thu Apr 7 05:12:13 EDT 2005


Hi,

Is there a way to compare an object against null with a user defined
operator?

In mono 1.0.4 i did get operator == called even if comparing against null.
In mono 1.1.6 that doesn't happen anymore.
Is this according to the standard? If it is how do i overload comparison
against null?

using System;

public class Test
{
        static public bool operator == (Test x, Test y)
        {
                System.Console.WriteLine ("The operator == is being
called");
                return false;
        }

        static public bool operator != (Test x, Test y)
        {
                System.Console.WriteLine ("The operator != is being
called");
                return true;
        }
        
        public override bool Equals (object o)
        {
                System.Console.WriteLine ("Equals function is being
called");
                return false;
        }

        public override int GetHashCode ()
        {
                System.Console.WriteLine ("Hash function is being called");
                return 0;
        }
        
};

public class Bug
{
  static void Main(string[] args)
  {
    Test t = new Test ();
    if (t == null)
    {
                System.Console.WriteLine ("The operator correctly returned
false");
    }
    else
    {
                System.Console.WriteLine ("The operator is never called");
    }
  }
}





More information about the Mono-devel-list mailing list