1

For a given string "5", if I use the built in GetHashCode() function what is the value that is returned? Am I confused in that it returns the integer value of 5?

user3743340
  • 47
  • 1
  • 6

2 Answers2

5

It's implementation specific, and you should not rely on anything you happen to observe in one particular implementation. Only rely on what is guaranteed: two equal strings will return the same value, in the same process. The same string value can return a different hash next time you run your program, or on a different machine. This means you should never persist the result of GetHashCode - it's not useful for future comparisons.

If two strings return the same hash code they may be equal - but they may not be.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • did't got your last line..please can you explain more? – Ehsan Sajjad Sep 09 '14 at 05:33
  • there can be situations where two different strings produce the same hashcode (indeed there always will be) – Random Dev Sep 09 '14 at 05:34
  • Basically, hash codes can collide. If x.GetHashCode() is not equal to y.GetHashCode, then x and y are definitely not equal. If the two hashes are the same, they still *might* be unequal. – Jon Skeet Sep 09 '14 at 05:35
  • @EhsanSajjad see https://en.wikipedia.org/wiki/Pigeonhole_principle :D ... there are more strings than there are possible hash-values ;) – Random Dev Sep 09 '14 at 05:36
  • @Justcode ??? - what do you mean by: *gurantee* and **perfect** output? (What do you consider as perfect?) - What should the 2 fields be in the case of a string? – Random Dev Sep 09 '14 at 07:03
  • what tweaks(?) - the collisions? This is a unavoidable *problem* when you use hashes – Random Dev Sep 09 '14 at 07:11
  • @CarstenKönig I think I got it on *msdn* thanks for your response though – Just code Sep 09 '14 at 07:14
1

For string.GetHash() MSDN Docs writes:

If two string objects are equal, the GetHashCode method returns identical values. However, there is not a unique hash code value for each unique string value. Different strings can return the same hash code.

The hash code itself is not guaranteed to be stable. Hash codes for identical strings can differ across versions of the .NET Framework and across platforms (such as 32-bit and 64-bit) for a single version of the .NET Framework. In some cases, they can even differ by application domain.

As a result, hash codes should never be used outside of the application domain in which they were created, they should never be used as key fields in a collection, and they should never be persisted.

Finally, do not use the hash code instead of a value returned by a cryptographic hashing function if you need a cryptographically strong hash. For cryptographic hashes, use a class derived from the System.Security.Cryptography.HashAlgorithm or System.Security.Cryptography.KeyedHashAlgorithm class.

So its kind of "quick-compare-check" feature regarding strings. But you should not relay on the hash-only. Its important to know that these hash-codes are not stable, meaning you must never store them in files, databases etc. - don't persist them.

In general GetHash() is specific to the class implementation as Jon wrote. If we look at the MSDN Docs for object.GetHash() we see that they serve as a index for hash-based collection so the collections index-tree is balanced. See this article for more information on hasing-algorithm.

So if you query one and the same object using GetHash() it should return the same hash-code. That code may be different if your application runs the next time.

Marc
  • 4,715
  • 3
  • 27
  • 34