Manthan
Manthan

Reputation: 383

Creating overload for String.GetHashCode() to return same value in x86 and x64 environments

I have a scenario in which my server side code runs in x86 environment where as the client side runs in x64. The issue is happening that we are using String.GetHashCode() to identify the objects within the HashTable since "The behavior of GetHashCode is dependent on its implementation, which might change from one version of the common language runtime to another. " -MSDN. I used the unsafe code to create x86 equivalent GetHashCode() from the following stack overflow question: Can I depend on the values of GetHashCode() to be consistent?.

I had a few questions:

  1. Is it recommended to leave unsafe code within managed code world.
  2. How to convert the unsafe code in the above mentioned question into safe code.

Upvotes: 3

Views: 542

Answers (1)

Manthan
Manthan

Reputation: 383

Created and extension method that generates a HashCode for the provided string so that it is same even if the client and server are being executed in different environments (x86 or x64).

Upvotes: 1

Related Questions