Hi all,
But the important quote: A class that implements IEquatable<T> is saying, “I know how to compare two instances of type T or any type derived from T for equality.” After all, if type B derives from A, then B is-a A. That’s what inheritance means! The problem with implementing IEquatable<T> on a non-sealed type is that a base class cannot know how to compare instances of derived types. Trying to implement IEquatable<T> on a non-sealed class ends up producing inconsistent results when comparing instances of derived types.
Here is an example.
class Base : IEquatable<Base> {
bool Equals(Base b) => return true;
}
class A : Base, IEquatable<A> {
bool Equals(A a) => return false;
}
class B : Base, IEquatable<B> {
bool Equals(B b) => return false;
}
A a = new A();
B b = new B();
Console.WriteLine(a.Equals(b)); // This should obviously return false as they are different classes however because of the existance of Base:Equals(Base b) it will return true.
Thanks,
Mike