Click here to Skip to main content
12,949,816 members (70,311 online)
Rate this:
Please Sign up or sign in to vote.
See more:

Sample code:

Hashtable h0 = new Hashtable();
Hashtable h1 = new Hashtable();



Do I need to get the h0, remove the items in h0, then remove the h0 from h1? Or just remove the h0?

Posted 14-Aug-09 16:12pm
Updated 25-Aug-09 10:32am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


no you don't need to dismantle an element that you want to remove from a collection.

BTW: you should consider using generic collections (such as Dictionary<T1,T2>), available since .NET 2.0; they are faster and safer.


Rate this: bad
Please Sign up or sign in to vote.

Solution 2

The hashtable only contains a reference to the object, not the object itself. Removing one hashtable reference from the other hastable (h0 from h1) only takes the reference to h0 out of h1. h0 itself remains unchanged (as do all references contained in h0).
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

I assume your question is about garbage collection. If so, just remove h0. Here's why...

By removing h0 from h1, you tell the garbage collector "hey, h0 is no longer referenced by anything, so you can garbage collect it". Once h0 is garbage collected, that means the boxed integer (10) is also not referenced, making it a candidate for garbage collection as well.

Garbage collection can be summarized as follows: if an object is no longer referenced by another object, it will be garbage collected (aka, released from memory). If there is some sort of circular referencing (such as variable A referencing variable B which then references variable A), then those objects will still be garbage collected so long as they aren't referenced by anything that the main threads can access.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 5,749
CHill60 3,380
Maciej Los 2,953
Jochen Arndt 1,935
ppolymorphe 1,820

Advertise | Privacy | Mobile
Web02 | 2.8.170524.1 | Last Updated 21 Nov 2009
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100