问题
In the implementation of GetHashCode
below, when Collection
is null
or empty will both result in a hash code of 0
.
A colleague suggested return a random hard coded number like 19
to differentiate from a null
collection. Why would I want to do this? Why would I care that a null
or empty collection produces a different hash code?
public class Foo
{
public List<int> Collection { get; set; }
// Other properties omitted.
public int override GetHashCode()
{
var hashCode = 0;
if (this.Collection != null)
{
foreach (var item in this.Collection)
{
var itemHashCode = item == null ? 0 : item.GetHashCode();
hashCode = ((hashCode << 5) + hashCode) ^ itemHashCode;
}
}
return hashCode;
}
}
回答1:
The design of GetHashCode
is that it is supposed to minimize the number of collisions that will take place, as best as it can. While having some hash collisions is inevitable, you'll want to be mindful of what types of objects are colliding, what type of data are going to be stored in your hash based collections, and working to ensure that types of objects stored together in the same collection are less likely to collide.
So if you happen to know something about how hash-based collections of this type are going to be used, and that there are likely to be both null and empty objects in them, then it would improve the performance to have them not collide. If you suspect that having both a null and empty value in the same collection is not particularly likely, then having them collide isn't actually a concern.
来源:https://stackoverflow.com/questions/56557128/null-vs-empty-collections-in-gethashcode