Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Overriding the GetHashCode method

In this example, the poster has overridden the get hash code method. I understand that this has been done in order to provide a better hash value for the returned object, to reduce the number of collisions, and therefore reduce the number of occasions it will be necessary to call Equals().

What i would like to know, is how this algorithm been calculated:

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

Is there a particular reason that the numbers in question were chosen? Could i have simply picked my own numbers to put into it?

like image 377
Richard Avatar asked May 31 '11 10:05

Richard


1 Answers

Generally you should choose primes. This helps you to avoid getting the same hash-value for different input parameters.

like image 161
Oleg Avatar answered Oct 05 '22 11:10

Oleg