I am aware of the fact that I always have to override Equals(object)
and GetHashCode()
when implementing IEquatable<T>.Equals(T)
.
However, I don't understand, why in some situations the Equals(object)
wins over the generic Equals(T)
.
For example why is the following happening? If I declare IEquatable<T>
for an interface and implement a concrete type X
for it, the general Equals(object)
is called by a Hashset<X>
when comparing items of those type against each other. In all other situations where at least one of the sides is cast to the Interface, the correct Equals(T)
is called.
Here's a code sample to demonstrate:
public interface IPerson : IEquatable<IPerson> { }
//Simple example implementation of Equals (returns always true)
class Person : IPerson
{
public bool Equals(IPerson other)
{
return true;
}
public override bool Equals(object obj)
{
return true;
}
public override int GetHashCode()
{
return 0;
}
}
private static void doEqualityCompares()
{
var t1 = new Person();
var hst = new HashSet<Person>();
var hsi = new HashSet<IPerson>();
hst.Add(t1);
hsi.Add(t1);
//Direct comparison
t1.Equals(t1); //IEquatable<T>.Equals(T)
hst.Contains(t1); //Equals(object) --> why? both sides inherit of IPerson...
hst.Contains((IPerson)t1); //IEquatable<T>.Equals(T)
hsi.Contains(t1); //IEquatable<T>.Equals(T)
hsi.Contains((IPerson)t1); //IEquatable<T>.Equals(T)
}
hst.Contains((IPerson)t1);
fail to compile? – MercymerdithHashSet<T>.Contains
butEnumerable.Contains
which accepts the interface. So this is the slow version that enumerates all elements and compares them withEquals(IPerson other)
instead of usingGetHashCode
. – Barramusing System.Linq;
– Mercymerdith