Will this hash function collide unusually frequently?

Yes Just suppose Prop1, Prop2 etc are of type int Usually only the lower range of integers is used. Your sum approach will collide more often than necessary The HasCode of 7 is 7, which makes perfect sense when hashing int by it self. But with your code the tuples 7, 3 3, 7 and 8, 2 will all have the same Hash.

The same with simple XOR instead of Addition The common approach is to add some (prime) numbers and shifting: public int GetHashCode(MyType obj) { int hash = 0; unchecked { hash += 19 * obj. Prop1.GetHashCode(); hash += 31 * obj. Prop2.GetHashCode(); hash += 37 * obj.

Prop3.GetHashCode(); } return hash; } The numbers 19, 31, 37 are not too critical. And if you prefer you can use OR or XOR instead of.

Yes. Just suppose Prop1, Prop2 etc are of type int. Usually only the lower range of integers is used.

Your sum approach will collide more often than necessary. The HasCode of 7 is 7, which makes perfect sense when hashing int by it self. But with your code the tuples , and will all have the same Hash.

The same with simple XOR instead of Addition. The common approach is to add some (prime) numbers and shifting: public int GetHashCode(MyType obj) { int hash = 0; unchecked { hash += 19 * obj. Prop1.GetHashCode(); hash += 31 * obj.

Prop2.GetHashCode(); hash += 37 * obj. Prop3.GetHashCode(); } return hash; } The numbers 19, 31, 37 are not too critical. And if you prefer you can use OR or XOR instead of + .

1 Prime numbers are good and are preferable to shifting, since a simple binning algorithm may well just take the lower N bits of the HashCode; if the properties are shifted, they may end up completely ignored. – Dan Bryant Jun 8 at 22:33.

XORing would be better: public int GetHashCode(MyType obj) { return obj. Prop1.GetHashCode() ^ obj. Prop2.GetHashCode() ^ obj.

Prop3.GetHashCode(); }.

1 See Henk Holterman's reasoning. Mixing with shifts should provide better distribution if GetHashCode for some of the properties does not use whole range... – Alexei Levenkov Jun 8 at 22:14.

You can use a modified FNV HashCode generator, a very similar question has been answered (by me) here.

Your sum approach will collide more often than necessary. The HasCode of 7 is 7, which makes perfect sense when hashing int by it self. But with your code the tuples , and will all have the same Hash.

The same with simple XOR instead of Addition. The numbers 19, 31, 37 are not too critical.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions