Newly introduced record types allow to override EqualityContract for type which makes possible to create a situation when two objects are equal but have different hashcodes which opposes guidelines for GetHashCode overriding:
If you override the GetHashCode method, you should also override Equals, and vice versa. If your overridden Equals method returns true when two objects are tested for equality, your overridden GetHashCode method must return the same value for the two objects.
public record Base(string Foo);
public record Child(string Foo, string Bar) : Base(Foo)
{
protected override Type EqualityContract => typeof(Base);
}
var b = new Base("Foo");
var c = new Child("Foo", "Bar");
Console.WriteLine(b == c); // True
Console.WriteLine(b.GetHashCode() == c.GetHashCode()); // False
While removing the extra property from Child makes GetHashCode and Equals "match".
Obviously this can be fixed with overriding GetHashCode, but I wonder why overriding EqualityContract does not automatically lead to having GetHashCode return value "matching" Equals implementation? Or is there any other way to handle that except manual GetHashCode override?