
.NET 2.0+Implementing IEqualityComparer<T>
IEqualityComparer<T> is a generic interface defined in the .NET framework version 2.0 and used in many standard classes, including several LINQ operators. The interface can be implemented to allow customised comparison of values and objects.
IEqualityComparer<T>
The IEqualityComparer<T> interface was introduced into the .NET framework with version 2.0. It is used by many standard methods that carry out value comparisons. It allows you to define exactly how those comparisons should be made. For example, the static OrdinalIgnoreCase property of the StringComparer class returns a case-insensitive comparer for strings. If you were to compare strings that differed only in capitalisation using another comparer or the equality operator you may see different results.
You can create your own implementations of IEqualityComparer<T> that work with your own classes and structures. These can then be used with comparison methods, including LINQ standard query operators, to allow greater control of the results. In this article we will create such a class that compares customer objects. Rather than checking that the references for two Customer instances match, we will compare a unique ID property. If the IDs match we will deem the two Customers to be equal, even if the other properties differ.
Implementing the Interface
To begin, create a new console application and add the following Customer class. This is a very simple type containing three properties and no business logic. The properties define a unique identifier, a forename and a surname for each customer.
public class Customer
{
Guid _id;
string _forename;
string _surname;
public Guid Id
{
get { return _id; }
set { _id = value; }
}
public string Forename
{
get { return _forename; }
set { _forename = value; }
}
public string Surname
{
get { return _surname; }
set { _surname = value; }
}
}
Creating the Comparer
Next we can declare the comparer class. We'll name it, "CustomerComparer". The class must implement the IEqualityComparer<T> interface to allow it to be used with .NET classes that perform comparisons. In this case we'll fix the type parameter as Customer, as we won't need it to compare any other type.
public class CustomerComparer : IEqualityComparer<Customer>
{
}
IEqualityComparer<T> Members
The IEqualityComparer<T> interface defines two members, both of which are methods. These are:
- Equals. This method accepts two values or objects to be compared. If they are deemed to match, the method returns true. If not, it returns false. It is important that the order in which the objects are supplied does not affect the result and that if the same object is provided to both parameters the method returns true.
- GetHashCode. This method is used to generate an integer hash code for the single provided value or object. It is essential that the hash codes for two values that are deemed equal by the Equals method are the same. In some cases the method can return the hash code generated by the GetHashCode method of the argument. However, if two objects that are deemed equal generate different hash codes you must create the value in another manner. This is the case with the standard case-insensitive string comparers.
14 January 2012