473,397 Members | 1,972 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,397 software developers and data experts.

Problem: GetHashCode for string is different in 1.1 and 2.

Hello,

I am wondering why GetHashCode() for the string is using two differemt
algorithms in .NET Framework 1.1 and 2.

This is creating a big problem, because we relied on this hashcode as
unique. How can we fix this problem?
--
Mike
Sep 9 '06 #1
6 2410
Mike,

You are going to have to work around it, unfortunately. There is no
guarantee that the hashcode is going to be unique across different versions
of the framework, or even across calls to GetHashCode across different
invocations of the program.

The default implementation of GetHashCode for object just returns a
counter to the object reference. This will always be different across
invocations of any program for any type that doesn't override GetHashCode.

If you need a hash that is predictable, then you have to go with a
well-known algorithm. You should look in the System.Security.Cryptography
namespace, and use the MD5 or SHA1 algorithm for hashing your data. This
will give you predictable results every time.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mike9900" <Mi******@discussions.microsoft.comwrote in message
news:1D**********************************@microsof t.com...
Hello,

I am wondering why GetHashCode() for the string is using two differemt
algorithms in .NET Framework 1.1 and 2.

This is creating a big problem, because we relied on this hashcode as
unique. How can we fix this problem?
--
Mike

Sep 9 '06 #2
Mike9900 <Mi******@discussions.microsoft.comwrote:
I am wondering why GetHashCode() for the string is using two differemt
algorithms in .NET Framework 1.1 and 2.
Presumably because MS found a better algorithm - potentially the new
one is faster, has fewer collisions, or has a better distribution. I
don't know - but it's entirely reasonable for it to change.
This is creating a big problem, because we relied on this hashcode as
unique.
You shouldn't rely on a hashcode as being unique in the first place
(it's not - there can be multiple strings with the same hashcode) and
you shouldn't rely on a hashcode being the same even across two *runs*
necesasrily let alone between two versions of the framework.
How can we fix this problem?
Well, you can probably dig up the 1.1 implementation somewhere, but you
should urgently review your code to see where else you're making the
same inaccurate assumption. Hashcodes should be used as a quick "first
pass" to check for equality (where the hash codes being equal does
*not* mean that objects are necessarily equal - only that if the hash
codes are not equal, the objects should definitely not be equal) in a
way which allows you to find equal objects quickly.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 9 '06 #3
Thanks for reply.

I have many strings and I want them to be stored in a file and be idetified
by hashcode, so when the id is encountered the application would know of the
string. What is the correct approach and algorithm for it?
--
Mike
"Nicholas Paldino [.NET/C# MVP]" wrote:
Mike,

You are going to have to work around it, unfortunately. There is no
guarantee that the hashcode is going to be unique across different versions
of the framework, or even across calls to GetHashCode across different
invocations of the program.

The default implementation of GetHashCode for object just returns a
counter to the object reference. This will always be different across
invocations of any program for any type that doesn't override GetHashCode.

If you need a hash that is predictable, then you have to go with a
well-known algorithm. You should look in the System.Security.Cryptography
namespace, and use the MD5 or SHA1 algorithm for hashing your data. This
will give you predictable results every time.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mike9900" <Mi******@discussions.microsoft.comwrote in message
news:1D**********************************@microsof t.com...
Hello,

I am wondering why GetHashCode() for the string is using two differemt
algorithms in .NET Framework 1.1 and 2.

This is creating a big problem, because we relied on this hashcode as
unique. How can we fix this problem?
--
Mike


Sep 10 '06 #4
Mike9900 <Mi******@discussions.microsoft.comwrote:
Thanks for reply.

I have many strings and I want them to be stored in a file and be idetified
by hashcode, so when the id is encountered the application would know of the
string. What is the correct approach and algorithm for it?
Are you saying the file can only contain hashes of some form, not the
full string? Could you give a bit more information about what you're
trying to do? It's not entirely clear. Note that hashing is one way -
you can't go from a hashcode to a full string, the information just
isn't there.

If you have a lookup from some kind of hash (a better specified one
than GetHashCode, with a better chance of uniqueness - such as SHA1 or
MD5) to the string, then you could do it, but there may well be a
better solution. The more information you could give us, the better
we'll be able to help you.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 11 '06 #5
Hello,

Thanks for the reply. I am using MD5, but I am wondering if SHA1 is better.

My problem is this:

I have a set of string that I want to refer to them by a identifier, because
the string could be in a different languages. So, the identifier does not
care about the language and always refer to the same string.

--
Mike
"Jon Skeet [C# MVP]" wrote:
Mike9900 <Mi******@discussions.microsoft.comwrote:
Thanks for reply.

I have many strings and I want them to be stored in a file and be idetified
by hashcode, so when the id is encountered the application would know of the
string. What is the correct approach and algorithm for it?

Are you saying the file can only contain hashes of some form, not the
full string? Could you give a bit more information about what you're
trying to do? It's not entirely clear. Note that hashing is one way -
you can't go from a hashcode to a full string, the information just
isn't there.

If you have a lookup from some kind of hash (a better specified one
than GetHashCode, with a better chance of uniqueness - such as SHA1 or
MD5) to the string, then you could do it, but there may well be a
better solution. The more information you could give us, the better
we'll be able to help you.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 12 '06 #6
Mike9900 <Mi******@discussions.microsoft.comwrote:
Thanks for the reply. I am using MD5, but I am wondering if SHA1 is better.

My problem is this:

I have a set of string that I want to refer to them by a identifier, because
the string could be in a different languages. So, the identifier does not
care about the language and always refer to the same string.
Where does hashing come in? Why not just use a GUID or something
similar?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 12 '06 #7

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

Similar topics

8
by: Dmitri Shvetsov | last post by:
Hi All, Did somebody try to get the Hash code from byte array? The method exists, the program is compilable but...))) Try!-) Every time you can get the different code from the same array, and...
4
by: Guinness Mann | last post by:
resource.cs(8,15): warning CS0659: 'UA.LMS.Resource.Resource' overrides Object.Equals(object o) but does not override Object.GetHashCode() What does this mean? I mean I know what it says, but do...
3
by: Ronin | last post by:
Hi i am using following code which extracts information from XML file and creates an instance of class which it adds to hash table. problem is i am unable to extract information from hashtable :...
8
by: Matthias Kientz | last post by:
I have a class which should override the GetHashcode() function, which look like this: public class A { public string str1; public string str2; //other methods...
2
by: perspolis | last post by:
Hi I have a question about GetHashCode for strings. Is it unique for all string in diffrent OS? for example "test".GetHashCode () is the same for all of OS? thanks in advance
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
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...
8
by: Ashish Khandelwal | last post by:
-----See below code, string str = "blair"; string strValue = "ABC"; string str1 = "brainlessness"; string strValue1 = "XYZ"; int hash = str.GetHashCode() ; // Returns 175803953 int hash1 =...
6
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have a moderately complex class (about 10 different string and int type fields along with two different List<stringfields). I want to override Equals(), so I need to override GetHashCode()....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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:
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.