Is it any different from the CLR standpoint to implement IEqualityComparer
vs overriding the ==
operator for the property you would use in the IEqualityComparer<T>
? And if so, when would you use one against the other?
Edit
Ok it does make sense that the IEqaulityComparer used by the implementations of Hashtable - it slipped out of my mind when I was posting the question. So what about the extensions of Linq of IEnumerable. Does that mean that .net builds up a Hashtable when executing those kind of extension methods?
IEqualityComparer
is not equal
, equal is for object (instance method) but EqualityComparer is for decoration for example in linq you want do specific distinct:
personList.OrderBy(p=>p.ID).Distinct(new MyEqualityComparer())
and
class MyEqualityComparer: IEqualityComparer<Person>
{
public bool Equals(Person p1, Person p2)
{
if (p1.Age == p2.Age)
return true;
return false;
}
public int GetHashCode(Person p)
{
return p.Id.GetHashCode();
}
}
but equal is for Person:
public class Person
{
public int ID{get;set;}
public int Age{get;set;}
public override bool Equals(object o)
{
//do stuff
}
}
you can do any number of decoration by IEqualityComparer but you can't do this by instance method (you can write personList.Distinct(new AnotherComparer) ,...)