Question

How do I check for nulls reliably in C#, including when overloading the operators == and !=?

class MyClass
{
    public static bool operator ==(MyClass a, MyClass b) { return false; }
    public static bool operator !=(MyClass a, MyClass b) { return true; }
}

class Program {
    static void Main(string[] args) {
        MyClass x = new MyClass();
        MyClass y = null;
        if (x != null) { System.Console.WriteLine(x.ToString()); }
        // Next line throws a NullReferenceException
        if (y != null) { System.Console.WriteLine(y.ToString()); }
    }
}

I know the following options are available:

  • x != null Not reliable when overloading operators.
  • null != x Not reliable when overloading operators.
  • (x ?? null) != null The type still is MyClass, so it isn't reliable when overloading operators.
  • object.ReferenceEquals(x, null) Should be ok.
  • x.Equals(null) Won't work (of course, since we call a method on x, which can be null)
  • object o = x; o != null should use object's operator !=, so it should work.
  • Something else?

So, which method is the best in terms of reliability, speed, readbility, and which is the most idiomatic? Does Microsoft recommend any method in their documentation/coding standards/whatever?

Was it helpful?

Solution

You can use the static ReferenceEquals method of System.Object.

That's the technically correct solution. What you should do however, is to let your supplier of said class know that it is a buggy piece of shit and you won't work with it. It should not be your responsibility to fix other peoples bugs. It's enough work to fix one's own bugs.

OTHER TIPS

Does Microsoft recommend any method in their documentation/coding standards/whatever?

Yes Microsoft does recommend, ReferenceEqualsor casting to object before checking for null. otherwise == will be called recursively. This is what microsoft has to say.

A common error in overloads of operator == is to use (a == b), (a == null), or (b == null) to check for reference equality. This instead results in a call to the overloaded operator ==, causing an infinite loop. Use ReferenceEquals or cast the type to Object, to avoid the loop.

By casting to object it means,

if(((object)yourObject) == null)
{}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top