473,657 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3909
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.co m

"Nick" <ni*********@ho tmail.com> wrote in message
news:a0******** *************** ***@posting.goo gle.com...
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*********@ho tmail.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.co m>
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*********@ho tmail.com> wrote in message
news:a0******** *************** ***@posting.goo gle.com...
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
6803
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
8491
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 for? --
1
1710
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
3538
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 vesrion of our app as they will not be able to log in. I know, we shouldn't have dealt with hashcode in the first place, but we
25
2563
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 constants in Python language. There are some workarounds available, for example the const-module. To me, this looks quite cumbersome and very unintuitive. For the interpreter, in the efficiency-wise, I just cannot tell.
9
2086
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 internally strings are storing using hasing. Please clear my concept regarding hashing in ,net
1
3995
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 application/web services as server), I have some methods that retrieve datasets from web service, then i save them locally with WriteXML and I can work offline, making insert, update and delete... then I connect again and I send only the changed rows...
11
2310
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 in a HashMap.....For more details kindly look here
1
1676
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++) System.out.println(l); Set set = new HashSet(Arrays.asList(elements)); // set.addAll(Arrays.asList(l));
0
8413
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.