Hashtables

T

Trevor Hardwick

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
 
D

Dennis Myrén

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.
 
J

Jon Skeet [C# MVP]

Trevor Hardwick said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top