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?
Asked
Active
Viewed 61 times
-1

kdm1jkm
- 5
- 1
-
1Does this answer your question? [Good GetHashCode() override for List of Foo objects respecting the order](https://stackoverflow.com/questions/8094867/good-gethashcode-override-for-list-of-foo-objects-respecting-the-order) – MoonMist Jan 08 '20 at 02:31
1 Answers
-1
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.

MoonMist
- 1,232
- 6
- 22