Getting .NET 1.1 CLR String Hash Codes In The .NET 2.0 CLR

Everyone knows (or, should know) that values values retrieved from the GetHashCode method should not be persisted for later use, specially with strings, because:

The behavior of GetHashCode is dependent on its implementation, which might change from one version of the common language runtime to another. A reason why this might happen is to improve the performance of GetHashCode. [^]

Nevertheless, code that persist values retrieved from the GetHashCode method for later use can fall on your lap. And if you need to upgrade your code to, for example, use WCF or WF, you have a problem.

The usual solution would be to use Reflector to see how it was done in the 1.1 CLR and implement the same algorithm in the 2.0 runtime.

Unfortunately, the 1.1 implantation isn't managed.

Today, thanks to Nicole, I found System.Collections.Specialized.BackCompatibleStringComparer on the 2.0 CLR. This class implements the 1.1 CLR String.GetHashCode algorithm and can be found in the System.dll and System.Windows.Forms.dll assemblies. You can't use it because it's internal but you can see its implementation using Reflector. You can also see it here.

Now I have a few questions:

Comments are closed.