Customizing Hashtable

  • Thread starter Thread starter Philipp Schumann
  • Start date Start date
P

Philipp Schumann

Hi,

I use a custom IHashCodeProvider with a Hashtable. What would your take be
on the question what this should return to indicate "invalid" (as for null
references) -- zero?

Thanks,
Phil
 
Philipp Schumann said:
I use a custom IHashCodeProvider with a Hashtable. What would your take be
on the question what this should return to indicate "invalid" (as for null
references) -- zero?

You should throw an ArgumentNullException, as per the documentation.
The normal Hashtable should never pass you a null reference though.
 
I know---I should have been more clear: what is in the Hashtable are
WeakReferences (I want to prevent the Hashtable from preventing garbage
collections of the objects it is referencing). So I want to return the
GetHashTable of a weakReference.Target, which may be null...
 
Philipp Schumann said:
I know---I should have been more clear: what is in the Hashtable are
WeakReferences (I want to prevent the Hashtable from preventing garbage
collections of the objects it is referencing). So I want to return the
GetHashTable of a weakReference.Target, which may be null...

So the key itself isn't null, it's just a property of the key which is
null. In that case you could return whatever you want. There's a
difficulty here though - that hashcode function effectively changes
over time. If you put a weak reference in the hashtable as a key, then
if the object the weak reference refers to is garbage collected, you
won't be able to find that key again, because the hashcode you're going
to return has changed.
 
Back
Top