I made a class that include List<T>
.(T
is a struct I made.) And I want to override Equals()
and GetHashCode()
. So I need to make HashCode from List<T>
. How can I make good HashCode from ordered list?
Jon Skeet provides a great example on a similar post:
public override int GetHashCode()
{
unchecked
{
int hash = 19;
foreach (var foo in foos)
{
hash = hash * 31 + foo.GetHashCode();
}
return hash;
}
}
Primes are generally very useful when hashing. If you're interested to know why, you'll have to look up the maths because people are most likely better at explaining that than I am.