Why are there two overloads of Equals in .NET’s object class? If I want to have a custom equality function, e.g. so that I can use sets or dictionaries, should I override both (in addition to GetHashCode), or it is enough to override just one of them.
Why are there two overloads of Equals in .NET’s object class? If I want
Share
You can’t override the static version.
The reason for the static version is so that you can call
object.Equals(myObject, myOtherObject)without checking for nulls beforehand.Internally, it just checks for nulls (returning
trueif both objects are null), then delegates tomyObject.Equals(myOtherObject). So overriding the non-static Equals method is all you need.