PC Review


Reply
Thread Tools Rate Thread

Customizing Hashtable

 
 
Philipp Schumann
Guest
Posts: n/a
 
      9th Apr 2005
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


 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      9th Apr 2005
Philipp Schumann <(E-Mail Removed)> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Philipp Schumann
Guest
Posts: n/a
 
      9th Apr 2005
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...

"Jon Skeet [C# MVP]" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> Philipp Schumann <(E-Mail Removed)> wrote:
>> 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.
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      9th Apr 2005
Philipp Schumann <(E-Mail Removed)> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HashTable.Add Modifies HashTable Items? CoderHead Microsoft Dot NET Framework 8 14th Sep 2006 09:23 PM
Hashtable.Clone() is shadow copy, how to depth copy hashtable? harvie wang Microsoft C# .NET 6 29th Oct 2005 04:36 AM
Customizing Hashtable, question 2 Philipp Schumann Microsoft Dot NET Framework 3 9th Apr 2005 08:54 PM
Q: syntax for Hashtable within a Hashtable? Matt C. Microsoft C# .NET 4 20th Aug 2004 09:37 PM
copy Hashtable values to another Hashtable? M Microsoft C# .NET 3 17th Oct 2003 08:02 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:02 AM.