Constant Hashcode

N

Nick

Hi all,
I am using GetHashCode on unique strings to get a unique integer for a
string that I can then place into a database (use int rather than the
string to make indexing faster). The problem is that the hashcode can
change depending ion the clr version, from msdn
'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. If you require the behavior of GetHashCode be
constant, override the runtime implementation of GetHashCode with an
implementation of your own that you know will never change.'

Does any one have the code so that I can overide gethashcode for my
string object so that my hashcode will remain constant even in the
future or can anyone suggest a better way of getting a unique int for
unquie strings.

Thanks,
Nick
 
N

Nicholas Paldino [.NET/C# MVP]

Nick,

You can not do this because the String class is sealed, so you can not
override the GetHashCode method. If anything, you will have a method that
generates a hash code itself, and use that.

I really wouldn't depend on it though. Chances are the implementation
of GetHashCode for the String object isn't going to change anytime soon, and
I don't think you will be running your app on a different platform anytime
soon either. If anything, funnel all the calls for the hashcode through a
method, and then by default, have it call GetHashCode on the string. This
way, if something does change (and you want to protect yourself against it),
you just have to change the implementation of the one method, instead of
finding everywhere that GetHashCode is called on a string.

Hope this helps.
 
J

Jon Skeet [C# MVP]

Nick said:
I am using GetHashCode on unique strings to get a unique integer for a
string that I can then place into a database (use int rather than the
string to make indexing faster). The problem is that the hashcode can
change depending ion the clr version, from msdn
'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. If you require the behavior of GetHashCode be
constant, override the runtime implementation of GetHashCode with an
implementation of your own that you know will never change.'

Does any one have the code so that I can overide gethashcode for my
string object so that my hashcode will remain constant even in the
future or can anyone suggest a better way of getting a unique int for
unquie strings.

Well, you could easily write your own routine to create a hashcode. It
won't be unique though - it can't be, as there are far more possible
strings than there are possible ints.
 
D

Dennis Myrén

I use this one:


public static int GetHashCode ( string value )
{
int h = 0;
for (int i = 0; i < value.Length; i ++)
h += value * 31 ^ value.Length - (i + 1);
return h;
}
 
R

Richard Blewett [DevelopMentor]

You also have the fairly major stumbling block that string is sealed so you'd have to use encapsulation and delegation instead.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog


Well, you could easily write your own routine to create a hashcode. It
won't be unique though - it can't be, as there are far more possible
strings than there are possible ints.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top