Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:

Sample code:

Hashtable h0 = new Hashtable();
h0.add(0,10);
Hashtable h1 = new Hashtable();
h1.add(0,h0);

h1.Remove(0);

Question:

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

Posted
Updated 25-Aug-09 10:32am
v2

Hi,

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.

:)

 
Share this answer
 
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).
 
Share this answer
 

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.

 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900