Overloading ==, checking for null

Jun 14 2004 2:17 PM
Suppose that I have a Foo class for which I've overloaded the == operator: // Class foo. public static bool operator == (Foo foo1, Foo foo2) { return (foo1.data == foo2.data); } Now I do this: Foo foo = something(); if ( foo == null ) ... Oops: NullReferenceException, because the overloaded == operator was used, and it tried to dereference the "null" that I passed in. So how do I test for a null value if the == operator is overloaded? Sure, I could try to use it and then catch the NullReferenceException, but is there a more elegant solution?

Answers (1)