I want to use the HashSet.Contains method because its super fast.
var hashset = new HashSet<Customer>(customers, new CustomerComparer());
var found = hashset.Contains(new Customer{ Id = "1234", Name = "mit" }); // "mit" instead of an equals "smith" in the comparer.
I am searching for multiple properties on the customer object.
I have to implement the IEqualityComparer interface like:
public class CustomerComparer : IEqualityComparer<Customer>
{
public bool Equals(Customer x, Customer y)
{
return x.Id == y.Id && x.Name.Contains(y.Name);
}
public int GetHashCode(Customer obj)
{
return obj.Id.GetHashCode() ^ obj.Name.GetHashCode();
}
}
Why is the Equals method never hit when I do NOT use an Equals method inside the CustomerComparer Equals method like the .Contains?