IEqualityComparer is a .NET framework interface that allows the implementation of customized equality comparison for collections. That is, you can create your own definition of equality, and specify that this definition be used with a collection type that accepts the IEqualityComparer interface. Supported in .NET versions 3.5, 3.0, 2.0 (Source: MSDN)
This interface allows the implementation of customized equality comparison for collections. That is, you can create your own definition of equality, and specify that this definition be used with a collection type that accepts the IEqualityComparer interface. In the .NET Framework, constructors of the Hashtable, NameValueCollection, and OrderedDictionary collection types accept this interface.
This interface supports only equality comparisons. Customization of comparisons for sorting and ordering is provided by the IComparer interface.
For the generic version of this interface, see System.Collections.Generic.IEqualityComparer(Of T).
The following code example demonstrates the implementation of a case-insensitive IEqualityComparer. In this example, the CaseInsensitiveComparer.Compare method is used to determine whether two objects are equal, based on the provided CultureInfo.
class myCultureComparer : IEqualityComparer
{
public CaseInsensitiveComparer myComparer;
public myCultureComparer()
{
myComparer = CaseInsensitiveComparer.DefaultInvariant;
}
public myCultureComparer(CultureInfo myCulture)
{
myComparer = new CaseInsensitiveComparer(myCulture);
}
publicnewbool Equals(object x, object y)
{
if (myComparer.Compare(x, y) == 0)
{
returntrue;
}
else
{
returnfalse;
}
}
publicint GetHashCode(object obj)
{
return obj.ToString().ToLower().GetHashCode();
}
}
Source: MSDN