07-20-2023, 02:07 PM
To quote from [Guidelines and rules for GetHashCode][1] by Eric Lippert:
> Rule: **Consumers of GetHashCode cannot rely upon it being stable over time or across appdomains**
>
> Suppose you have a Customer object
> that has a bunch of fields like Name,
> Address, and so on. If you make two
> such objects with exactly the same
> data in two different processes, they
> do not have to return the same hash
> code. If you make such an object on
> Tuesday in one process, shut it down,
> and run the program again on
> Wednesday, the hash codes can be
> different.
>
> This has bitten people in the past.
> The documentation for
> System.String.GetHashCode notes
> specifically that two identical
> strings can have different hash codes
> in different versions of the CLR, and
> in fact they do. **Don't store string hashes in databases and expect them to be the same forever, because they won't be.**
So what is the correct way to create a HashCode of a string that I can store in a database?
(Please tell me I am not the first person to have left this bug in software I have written!)
[1]:
> Rule: **Consumers of GetHashCode cannot rely upon it being stable over time or across appdomains**
>
> Suppose you have a Customer object
> that has a bunch of fields like Name,
> Address, and so on. If you make two
> such objects with exactly the same
> data in two different processes, they
> do not have to return the same hash
> code. If you make such an object on
> Tuesday in one process, shut it down,
> and run the program again on
> Wednesday, the hash codes can be
> different.
>
> This has bitten people in the past.
> The documentation for
> System.String.GetHashCode notes
> specifically that two identical
> strings can have different hash codes
> in different versions of the CLR, and
> in fact they do. **Don't store string hashes in databases and expect them to be the same forever, because they won't be.**
So what is the correct way to create a HashCode of a string that I can store in a database?
(Please tell me I am not the first person to have left this bug in software I have written!)
[1]:
[To see links please register here]