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.
Dispose()
onIDisposable
resources, or ensure thatDispose()
is called by some other code. – AsyllabicToolStripItemCollection.Clear()
actually callsDispose
on the collection items or not. If you look at the disassembled source you will see that it doesn't. – Paperweightvar item
won't work, you needIDisposable
for callingDispose
. – MailboxToolStripItem
actually uses unmanaged resources or not is an implementation detail. The fact is, it implementsIDisposable
, and the contract ofIDisposable
says "This object MAY directly or indirectly use some unmanaged resources, and you should dispose of it whenever you are done with it." – Dickens