How do I deal with null fields in GetHashCode
function?
Module Module1
Sub Main()
Dim c As New Contact
As Jeff Yates suggested, the override in the answer would give the same hash for (name = null, address = "foo") as (name = "foo", address = null). These need to be different. As suggested in link, something similar to the following would be better.
public override int GetHashCode()
{
unchecked // Overflow is fine, just wrap
{
int hash = 17;
hash = hash * 23 + (Name == null ? 0 : Name.GetHashCode());
hash = hash * 23 + (Address == null ? 0 : Address.GetHashCode());
}
return hash;
}
What is the best algorithm for an overridden System.Object.GetHashCode?