For any objects references X
and Y
, regardless of their types, it is possible to meaningfully ask and answer the question "Is the object referred to by X
equivalent to the one referred to by Y
". If X
represents a Porche 911 automobile and Y
represents a cast-iron park bench, the answer would simply be "no". To be sure, if one knew that X
was a car and Y
a bench, one likely wouldn't bother asking, but suppose X
, Y
, or both, were "things that one may be asked to paint". One might not know whether or not X
and Y
are of the same type, and the objects are not equivalent, one may not care. Having a universal means of asking equivalence saves code the trouble of having to worry about objects' exact type.
The reason to have all objects implement Equals
as a virtual method is that it's the easiest mechanism by which objects can supply a definition of equivalence that is broader than referential equality. It is often useful to have immutable objects report themselves as equivalent to other objects which have the same immutable state [e.g. having two strings, both holding the six characters "GEORGE", reported each other as equivalent] Having all objects implement Equals
as a virtual method, and having mutable objects' implementation simply report referential equality, is generally easier than having an Equals
function which can only be used on immutable objects. After all, it's not hard for an mutable object to simply report itself as unequal to anything other than itself.