So I have some code that is failing because two string literals have HashCodes that can evaluate to the same thing. And while I appreciate that collision can happen I wasn't quite expecting this. But whilst debugging an issue in code a colleague and I found that in an immediate window if we did
"55d02ProductAd".GetHashCode() == "55b0tProductAd".GetHashCode()
It would evalute to true. Not ideal, but not impossible. When we described this to another colleague, in his disbelief he wrote up a scratch program that did
var h1 = "55d02ProductAd".GetHashCode();
var h2 = "55b0tProductAd".GetHashCode();
Console.WriteLine(h1 == h2);
In the above, they do not evaluate to the same thing. We have our monitors next to each other and we are confused as to the different outputs. Any thoughts?
You should never persist or use a hash code outside the application domain in which it was created, because the same object may hash across application domains, processes, and platforms.