I read a lot about this interesting topic (IMO). but I don’t quite understand one thing:
The size of a dictionary increases its capacity (doubles to the nearest prime) to a prime (when redistributing): because:
int index = hashCode % [Dictionary Capacity];
- So we can see that primes are used here for
[Dictionary Capacity], because their GreatestCommonFactor 1. and it helps to avoid collisions.
Besides
I have seen many implementation examples GetHashCode():
Here is an example 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 do not understand:
Question
Are primes used in both : Dictionary capacity and generation getHashCode?
, [, , ] -
: (11,17,173 - )
int hash = 17;
hash = hash * 23 + 11; //402
hash = hash * 23 + 17; //9263
hash = hash * 23 + 173 //213222
return hash;
213222 .
, :
(not a prime number) + (prime number) = (prime number)
(not a prime number) * (prime number) = (prime number)
(not a prime number) * (not a prime number) = (prime number)
, , ?