what is the best practice to overload == operator that compares two instances of the same class when it comes to null reference comparison?
MyObject o1 = null;
MyObject o2 = null;
if (o1 == o2) ...
static bool operator == (MyClass o1, MyClass o2)
{
// ooops! this way leads toward recursion with stackoverflow as the result
if (o1 == null && o2 == null)
return true;
// it works!
if (Equals(o1, null) && Equals(o2, null))
return true;
...
}
What is the best approach to handle null references in comparison ?
I’ve wondered if there is a “best approach”. Here’s how I do it: