By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,681 Members | 1,864 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,681 IT Pros & Developers. It's quick & easy.

Hashtables

P: n/a
Hi

I'm using a hashtable where the key is always an integer. The value stored
with the key holds another key that gives me a collection. My question is
will the hashcode generated for the key (an integer) always be unique i.e.
is there any chance given two different integers that I may get the same
hashcode?

If it is possible to get the same hashcode, does the .Net hashtable deal
with this e.g. when I pass in my unique key will it return me the correct
value associated with that key.

Regards
Jul 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In case of Int32, the hash code generated will be the value itself.
So, as long as you know the values are unique, you are fine.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Trevor Hardwick" <tr****@leavemealone.com> wrote in message
news:OK**************@TK2MSFTNGP14.phx.gbl...
Hi

I'm using a hashtable where the key is always an integer. The value
stored with the key holds another key that gives me a collection. My
question is will the hashcode generated for the key (an integer) always be
unique i.e. is there any chance given two different integers that I may
get the same hashcode?

If it is possible to get the same hashcode, does the .Net hashtable deal
with this e.g. when I pass in my unique key will it return me the correct
value associated with that key.

Regards

Jul 21 '05 #2

P: n/a
Trevor Hardwick <tr****@leavemealone.com> wrote:
I'm using a hashtable where the key is always an integer. The value stored
with the key holds another key that gives me a collection. My question is
will the hashcode generated for the key (an integer) always be unique i.e.
is there any chance given two different integers that I may get the same
hashcode?

If it is possible to get the same hashcode, does the .Net hashtable deal
with this e.g. when I pass in my unique key will it return me the correct
value associated with that key.


Dennis gave the answer in terms of the uniqueness of Int32 hashcodes,
but yes, Hashtable does (and *must*) cope with two different keys
returning the same hashcode - otherwise you couldn't use them for
things like strings, for a start.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.