Question

I use ToolStripMenuItem inside some of my projects and allocate it using C# operator new:

ToolStripMenuItem someMenuItem = new ToolStripMenuItem("Some Item");

I read MSDN documentation on IDisposable interface and using statement (http://msdn.microsoft.com/en-us/library/yh598w02.aspx). It is still unclear for me if ToolStripMenuItem have any underlying unmanaged resource or not.

More precisely, should I take care of call to Dispose(false) method if some exception occurs?

Was it helpful?

Solution

ToolStripMenuItems inherit from Component, which does implement the IDisposable interface.

Using the using() syntax for a ToolStripMenuItem wouldn't really work since you generally want the menu item to exist for the end user to interact with it, and the using() syntax would immediately dispose of it.

It's unclear to me what "some exception occurs" has to do with the disposal of the ToolStrip item. You shouldn't have to worry about disposing of it since it will be garbage collected when the hosted form gets closed.

Keep in mind though, that removing a ToolStripMenuItem from an items collection does not dispose of it, and in that case, you would want to call the dispose method yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top