Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
like image 251
Manthan Avatar asked Feb 02 '26 07:02

Manthan


1 Answers

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).

like image 115
Manthan Avatar answered Feb 03 '26 20:02

Manthan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!