I've been reading quite a lot about this interesting topic (IMO). but I'm not fully understand one thing :
Dictionary size is increasing its capacity ( doubles to the closest prime number) to a prime number (when reallocation) : because :
int index = hashCode % [Dictionary Capacity];
- So we can see that prime numbers are used here for
[Dictionary Capacity]
because their GreatestCommonFactor is1
. and this helps to avoid collisions.
In addition
I've seen many samples of implementing theGetHashCode()
:
Here is a sample from Jon Skeet :
public override int GetHashCode()
{
unchecked
{
int hash = 17;
// Suitable nullity checks etc, of course :)
hash = hash * 23 + field1.GetHashCode();
hash = hash * 23 + field2.GetHashCode();
hash = hash * 23 + field3.GetHashCode();
return hash;
}
}
I don't understand :
Question
Does prime numbers are used both in :
Dictionary capacity
and in the generation ofgetHashCode
?
Because in the code above , there is a good chance that the return value will not be a prime number [please correct me if i'm wrong] because of the
- multiplication by
23
- addition of the
GetHashCode()
value for each field.
For Example : (11,17,173 are prime number)
int hash = 17;
hash = hash * 23 + 11; //402
hash = hash * 23 + 17; //9263
hash = hash * 23 + 173 //213222
return hash;
213222 is not a prime.
Also there is not any math rule which state :
(not a prime number) + (prime number) = (prime number)
nor
(not a prime number) * (prime number) = (prime number)
nor
(not a prime number) * (not a prime number) = (prime number)
So what am I missing ?