In the msdn guidance on Equals override, why the cast to object in the null check?

The == operator may be overridden, and if it is, the default reference comparison may not be what you get. Casting to System.Object ensures that calling == performs a reference equality test.

public static bool operator ==(MyObj a, MyObj b)
{
  // don't do this!
  return true;
}

...
MyObj a = new MyObj();
MyObj b = null;
Console.WriteLine(a == b); // prints true
Console.WriteLine((object)a == (object)b); // prints false

Leave a Comment