Re: Comment on how to uniquely track your objects in C# / hash table / get hash code
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Tue, 5 Aug 2008 10:53:04 +0100
raylopez99 <raylopez99@xxxxxxxxx> wrote:
But, here is a philosophical problem to consider: how often does the
IL engine (sorry in advance for any wrong terminology) assign the same
hash code id to two different objects? Very, very rare, but it
happens. Then, when this happens, you'll get an error when you use
GetHashCode or Object.ReferenceEquals(), no? Has anybody thought
about this problem?
Yes, they certainly have. When you look something up in a hashtable,
first you need to find all the items that have the same hashcode as the
key that you're trying to look up. Then you need to call Equals to find
out whether each candidate entry is *actually* a match for the key
you're using.
Hash collisions slow things down (because you need to call Equals on
more candidate entries) but they don't stop a hashtable from actually
working correctly. Just don't try to treat a hashcode as a unique
identifier - it isn't.
--
Jon Skeet - <skeet@xxxxxxxxx>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
.
- Follow-Ups:
- References:
- Comment on how to uniquely track your objects in C# / hash table / get hash code
- From: raylopez99
- Re: Comment on how to uniquely track your objects in C# / hash table / get hash code
- From: Arne Vajhøj
- Re: Comment on how to uniquely track your objects in C# / hash table / get hash code
- From: raylopez99
- Comment on how to uniquely track your objects in C# / hash table / get hash code
- Prev by Date: Re: Comment on how to uniquely track your objects in C# / hash table / get hash code
- Next by Date: Re: Comment on how to uniquely track your objects in C# / hash table / get hash code
- Previous by thread: Re: Comment on how to uniquely track your objects in C# / hash table / get hash code
- Next by thread: Re: Comment on how to uniquely track your objects in C# / hash table / get hash code
- Index(es):
Relevant Pages
|