Alex Rivera | Logout

Does calling Clear disposes the items also?

Asked 2009-12-28T11:23:15.783
25

Many times there is a clear method, that removes all the items from the collections, are these items disposed also.

Like,

toolStripMenuItem.DropDownItems.Clear();

is sufficient, or should I have to call like that:

foreach (ToolStripItem item in toolStripMenuItem.DropDownItems)
{
  toolStripMenuItem.DropDownItems.Remove(item);
  item.Dispose();
}

Edit: Well ToolStripItem is an example not a question, for those who says Clear is enough I found another example, TabControl has also item collection and clear method. But TabControls can have complex controls (at least I have), which needs to be explicitly Dispose (even if they are Disposed automatically at some point by GC, cause they take huge memory). I guess the best answer is divo comment to dispose the items, and then call clear.

Edit
Report

1 Answer

0

I don't think so,more, it can cause many logical problems because you may have reference to that object in the collection for later use. If you don't have references to that objects Garbage Collector will dispose that objects later

answered 2009-12-28T11:25:01.817

Your Answer