Below is a sample implementation of overriding Object.Equals() for an entity base class from which all other entities in an application derive.
All entity classes have the property Id, which is a nullable int. (It's the primary key of whatever table the entity class corresponds to.)
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
return false;
if (base.Equals(obj))
return true;
return Id.HasValue && ((EntityBase) obj).Id.HasValue &&
Id.Value == ((EntityBase) obj).Id.Value;
}
Given this implementation of Equals(), how do you correctly implement GetHashCode()?