Dictionary <,> Size, GetHashCode and Prime Numbers?

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?

, [, , ] -

  • 23
  • 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)

, , ?

+5
1

, GetHashCode ( ), , . ( ) GetHashCode , ( ).

a b, , c = a * b. a b, c. , 6 * 2 = 12 4 * 3 = 12. , a , , . , - .

: . , . .


: Cicada ( ) , , . , , , .

+7

All Articles