Garbage collector / Rekursive Structure

P

Peter

Hi
i have a class (Item) and a typed collection
(ItemCollection:CollectionBase).
Each Item can have Sub-Items and each Item has a Reference to his
"Parent Item".

in brief:

public class Item
{
private ItemCollection subItems=new ItemCollection();
private Item parentItem = null;

public void AddSubItem(Item subItem)
{
subItem.ParentItem = this;
this.subItems.Add(subItem);
}
}


A "Structure starts" with a main-Item, adding SubItems, Sub-Sub-Items,
Sub-Sub-Sub-Items e.g. reading Data from Database or file).

public class Viewer
{
public void View()
{
Item mainItem = new Item();
// ... adding sub, sub-sub, ..... items
// do smth. with mainItem

}

}

Now my Question: can garbage collection remove mainItem, all sub-Items
and collections automaticly
should I do smth. special at the end ov Viewer.View() (beacuse of the
parent ref.), eg. setting alle "Parent-References" manually to null...

thank you
Peter.
 
A

Arne Vajhøj

Peter said:
i have a class (Item) and a typed collection
(ItemCollection:CollectionBase).
Each Item can have Sub-Items and each Item has a Reference to his
"Parent Item".
Now my Question: can garbage collection remove mainItem, all sub-Items
and collections automaticly

Yes.

If the entire data structure is no longer reachable all the objects
can be GC'ed.
should I do smth. special at the end ov Viewer.View() (beacuse of the
parent ref.), eg. setting alle "Parent-References" manually to null...

No.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top