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.
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
"Nick" <ni*********@hotmail.com> wrote in message
news:a0**************************@posting.google.c om...
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