Click here to Skip to main content
11,644,731 members (64,844 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#

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 14-Aug-09 16:12pm
elvis_pan1.1K
Edited 25-Aug-09 10:32am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.

Smile | :)

  Permalink  
Rate this: bad
good
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).
  Permalink  
Rate this: bad
good
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.

  Permalink  

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

  Print Answers RSS
0 OriginalGriff 680
1 jyo.net 494
2 Sergey Alexandrovich Kryukov 464
3 Afzaal Ahmad Zeeshan 404
4 CPallini 386
0 OriginalGriff 1,020
1 Sergey Alexandrovich Kryukov 673
2 DamithSL 656
3 Afzaal Ahmad Zeeshan 652
4 CPallini 555


Advertise | Privacy | Mobile
Web04 | 2.8.150731.1 | Last Updated 21 Nov 2009
Copyright © CodeProject, 1999-2015
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