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?

3
  • It does not really matter - why do you want to know (it is a implementation detail) Commented Sep 9, 2014 at 5:29
  • the return value is -842352757 [check this][1] [1]: stackoverflow.com/questions/7425142/… Commented Sep 9, 2014 at 5:33
  • ideone gives 53. Commented Sep 9, 2014 at 5:34

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

did't got your last line..please can you explain more?
there can be situations where two different strings produce the same hashcode (indeed there always will be)
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.
@EhsanSajjad see en.wikipedia.org/wiki/Pigeonhole_principle :D ... there are more strings than there are possible hash-values ;)
@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?
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.