Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Just to add some detail as to where the idea of a changing hashcode may have come from.</p> <p>As the other answers have rightly said the hashcode for a specific string will always be the same for a specific runtime version. There is no guarantee that a newer runtime might use a different algorithm perhaps for performance reasons.</p> <p>The String class overrides the default GetHashCode implementation in object.</p> <p>The default implementation for a reference type in .NET is to allocate a sequential ID (held internally by .NET) and assign it to the object (the objects heap storage has slot for storing this hashcode, it only assigned on the first call to GetHashCode for that object).</p> <p>Hence creating an instance of a class, assigning it some values then retrieving the hashcode, followed by doing the exact same sequence with the same set of values will yeild different hashcodes. This may be the reason why some have been led to believe that hashcodes can change. In fact though its the instance of a class which is allocated a hashcode once allocated that hashcode does not change for that instance.</p> <p><strong>Edit</strong>: I've just noticed that none of the answers directly reference each of you questions (although I think the answer to them is clear) but just to tidy up:-</p> <blockquote> <p>Can I be sure that the hash for a given string ("a very long string") will be always the same? </p> </blockquote> <p>In your usage, yes.</p> <blockquote> <p>Can I be sure that two different strings won't have the same hash?</p> </blockquote> <p>No. Two different strings may have the same hash.</p> <blockquote> <p>Also, if possible, how likely is it to get the same hash for different strings?</p> </blockquote> <p>The probability is quite low, resulting hash is pretty random from a 4G domain.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload