Looking at What is the best algorithm for an overridden System.Object.GetHashCode? I was struck that in many of the answers that suggest hashcodes of the type hash = hash*(prime) + item.GetHashcode()
that the value of hash is initially seeded to another prime rather than 0.
I understand the reason for the prime in the calculation portion coprime numbers are useful in many ways.
What I don't understand is why the hash is initialised to a non-zero number in the first place.
Looking at the precise example:
int hash = 17;
hash = hash * 23 + field1.GetHashCode();
hash = hash * 23 + field2.GetHashCode();
hash = hash * 23 + field3.GetHashCode();
return hash;
For shorthand lets let field1.GetHashCode() be represented with f1 (and so on for the others) and the initial hash value as i then this gives:
int hash = i;
hash = i * 23 + f1;
hash = (i * 23 + f1)* 23 + f2;
hash = ((i * 23 + f1)* 23 + f2)* 23 + f3;
Expanding the brackets in that last row:
hash = (i*23*23 + f1*23 + f2)* 23 + f3;
hash = i*23*23*23 + f1*23*23 + f2*23 + f3;
So as we can see the only effect of the initial hash value is to increase the final has value by a constant value of i*23*23*23 which would generalise to i*23^(number of fields).
So how does this help? In the event of f1, f2, f3 all being 0 is it a problem if the final hash were 0? Is it better for it to be something non-zero? My only thought is that implementations of things like dictionaries or hash sets that use the hash value prefer non-zero values for some reason but I can't think what that reason might be. Or the other things of course that these things are a little bit arcane so people use a tried and tested thing and so the initial value gets propagated even though there is no reason for it.
I tried looking up some microsoft hashcodes but the ones I found all used external code to calculate them (object, string) or were slightly special (the implementation of GetHashCode on anonymous objects seeds the hashcode based off of the property names of the anonymous objects which is different because it isn't a constant initial value).
So in summary why the initial constant value in hash code implementations?
Edit: Why use a prime number in hashCode? was suggested as a duplicate and the site wants me to edit my question to explain why it is not a duplicate... I have acknowledged that primes are used as the multiplier in the calculations and I understand why that is. This question is explicitly about the use as an initial seed in the hash code algorithm. The suggested duplicate doesn't explicitly say what the prime is used for but the answers all address the use of it as a multiplying factor which is not relevant to this question.