473,406 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Constant Hashcode

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
Nov 16 '05 #1
4 3871
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

Nov 16 '05 #2
Nick <ni*********@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
I use this one:
public static int GetHashCode ( string value )
{
int h = 0;
for (int i = 0; i < value.Length; i ++)
h += value [i] * 31 ^ value.Length - (i + 1);
return h;
}
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"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

Nov 16 '05 #4
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.

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: pentium77 | last post by:
Hi, Just wondering what's the algo used to compute hashcode in Java ? Does anybody here know ? Thanks, -MK.
3
by: Josema | last post by:
Hi to all, In a lot of type of classes in the .net framework classes library i see that has the method GethashCode. What is the HashCode of anything, and in wich kind of functionality are used...
1
by: Frank | last post by:
Hi, I save the hashcode of a datatablerow. How do I use that hascode to get to the original datarow? Thanks Frank
8
by: MuZZy | last post by:
Hi, Why for god sake they change implementation of String.GetHashCode() from ..NET 1 to .NET 2? We were storing some user passwords in hashcode, now we can't upgrade those clients with .NET 2...
25
by: tsaar2003 | last post by:
Hi Pythonians, To begin with I'd like to apologize that I am not very experienced Python programmer so please forgive me if the following text does not make any sense. I have been missing...
9
by: archana | last post by:
Hi all, I have one question regarding hashing in .net I have two string containing same data. When i see hashcode by calling gethascode, i am getting same value. I want to know how...
1
by: Ing. Davide Piras | last post by:
Hi there, I really don't know if I can post here or microsoft.public.dotnet.framework.adonet could be better... anyway: in my .NET 2.0 C# arcitecture (windows form as client application, web...
11
madhoriya22
by: madhoriya22 | last post by:
Hi, I have wrapped to strings in a object using hashCode of these strings.......Now I want to know how can I get back these strings from the object...............I am putting this object as a key...
1
by: praveenkumarvpk | last post by:
public static void main(String args) { String elements = { "A", "B", "C", "D", "E" }; Integer l = {1,2,68}; Integer y=1; int d=0; for(int i=0;i<l.length;i++) ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.