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.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?
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.