C # Overriding GetHashCode Method

In this example, the poster overrides the get hash code method. I understand that this was done in order to provide a better hash value for the returned object, to reduce the number of collisions and, therefore, to reduce the number of cases that Equals() will need to be called.

What I would like to know is a way to calculate this algorithm:

 return 17 + 31 * CurrentState.GetHashCode() + 31 * Command.GetHashCode(); 

Is there any special reason that the selected numbers were selected? Can I just select my own numbers to paste into it?

+6
source share
3 answers

As a rule, you should choose prime numbers. This will help you avoid getting the same hash value for different input parameters.

+4
source

Core numbers are commonly used in hash code calculations to minimize collisions. If you are looking for hashcode and primes on this icon, you will find detailed explanations about this (note that this is the language of the note):

+2
source

Usually you want to use prime numbers (as done above), because it reduces the likelihood of collisions (two copies give the same result). For more information see http://computinglife.wordpress.com/2008/11/20/why-do-hash-functions-use-prime-numbers/

+1
source

Source: https://habr.com/ru/post/889436/


All Articles