Hashtables

  • Thread starter Thread starter Trevor Hardwick
  • Start date Start date
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
 
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.
 
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.
 
Back
Top