C# String GetHashCode()

It is very difficult to Java developers to write C# code. Not because of the language – they are pretty much the same. But it turns out you make basic assumptions which are just not true.

We develop C# code that access services deployed in WebSphere ESB and send messages in WebSphereMQ. The sender creates a message identifier, that consists of the sender id, object type and more. The listener creates the same identifier, and listens to the queue with this identifier.
Suddenly – the identifier the listener and the sender create is different.
Turns out that the problem lies in the fact that the sender is a 32bit machine, and the listener runs on a 64bit machine.
Why is that a problem? Because in .net, code is not cross platform – not even between Windows environments – since the GetHashCode() function of String is not cross platform – and works differently on 32bit and 64bit machines.
Anyway, we wrote our own Hash function, that simply copies the java.lang.String hash() function (every JDK comes with src.zip, where you can find the java.lang.String code).
3 hours of my life wasted…

2 thoughts on “C# String GetHashCode()

  1. hi,First of all. Thanks very much for your useful post.I just came across your blog and wanted to drop you a note telling you how impressed I was with the information you have posted here.Please let me introduce you some info related to this post and I hope that it is useful for .Net community.There is a good C# resource site, Have alook http://csharptalk.comsimi


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s