GetHashCode – Does it return unique values for different inputs?
The Object class of the .NET framework has got a virtual method, say, GetHashCode that other classes could override to provide a custom implementation. This function is suitable for use in hashing algorithms like a hash table. The return value [of the default implementation] is and has never been meant to be used as a unique key. Although, an ideal implementation would guarantee that, however, it is impossible or impractical.
If you are going to implement your own GetHashCode method, you need to keep the following three criterias in your mind:
- If two objects of the same type represent the same value, the hash function must return the same constant value for either object.
- For the best performance, a hash function must generate a random distribution for all input.
- The hash function must return exactly the same value regardless of any changes that are made to the object.
Many people believe (unfortunately) that the default implementation of String.GetHashCode function returns a unique number for different input values. However, Mashiharu’s sample states that the two following different string, produces the same hash code [on .NET FX 2.0]:
Trace.WriteLine("0:93121".GetHashCode()); Trace.WriteLine("0:2546870".GetHashCode());
Having this in mind, the GetHashCode function could be implemented by XORing the field values with each other:
public struct MyStruct1 { public int field1; public int field2; public override int GetHashCode() { return field1 ^ field2; } }
Another common approach is to use a prime#:
public struct MyStruct2 { public int field1; public int field2; public override int GetHashCode() { return 19 * field1.GetHashCode() + 1 * field2.GetHashCode(); } }
where 19 is a prime#.
There are more complicated approaches each of which deserve a separate post. Whatever your approach is, just follow those above-mentioned rules. For more information, and some implementations of the GetHashCode, checkout the following sites:
- Microsoft Developer Network (MSDN)
- Frank Hileman’s notes on hash functions
- Ian Griffiths, Paul Hsieh & Mashiharu.
- Wikipedia
3 Responses to GetHashCode – Does it return unique values for different inputs?
Leave a Reply Cancel reply
Sponsors
Circle up with me on Google+
salam
man jahaye dige ham didam k migan az XOR masalan estefade beshe (msdn)
vali baraye in code bayad chikar beshe :
inja baraye p1,p3 k 2ta object e motafavet hastan meghdare Hashcode yeksan mide!! (shayad bayad yekam Salt behesh ezafe beshe baraye GetHashCode,are?)
Salam,
Hamoontori ke dar post asli neveshtam, GetHashCode hargez gharar nist ye adade Unique bargardoone, agarche ye Implementation e khoob baraye in function chenin chizi ro tazmin mikone, amma dar amal in masale sakht va omooman gheire momkene.
Pas bayad faghat say konim, ke paraakandegie khoobi hengaame generate kardane HashCode daashte baashim.
Be onvaane mesaal:
va dar nahaayat code.GetHashCode() ro mitoonid be onvaane khoroojiye function bargardoonid.
You have really interesting blog, keep up posting such informative posts!