I have implemented the class below:
public class carComparer : IEqualityComparer<Car>
{
public bool Equals(Car car1, Car car2)
{
if (car1 == null || car2 == null)
return false;
return (car1.description == car2.description);
}
public int GetHashCode(Car car)
{
unchecked
{
int hash = 17;
hash = hash * 29 + car.id.GetHashCode();
hash = hash * 29 + car.description.GetHashCode();
return hash;
}
}
}
Now see this:
Car p1 = new Car() { id = Guid.NewGuid(), description = "Test1" };
Car p2 = new Car() { id = Guid.NewGuid(), description = "Test1" };
Car p3 = new Car() { id = Guid.NewGuid(), description = "Test1" };
Car p4 = new Car() { id = Guid.NewGuid(), description = "Test1" };
var hash = new HashSet<Car>();
hash.Add(p1);
hash.Add(p2);
var hash2 = new HashSet<Car>();
hash2.Add(p3);
hash2.Add(p4);
var carComparer = new CarComparer();
Assert.That(hash, Is.EquivalentTo(hash2).Using(carComparer));
I put breakpoints in .equals and .hashcode. Equals is used; but GetHashCode is not. Why?