Wednesday, December 23, 2009

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.

Unbelievable!!!

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 comments:

  1. Wow man, this is so fucked up. Good for you! Why don't you also have a link to this post at http://www.alunasoft.com ?

    ReplyDelete
  2. 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.com

    simi

    ReplyDelete