很多时候,有一个明显的方法,可去除集合中的所有项目,在这些项目处置也。
Many times there is a clear method, that removes all the items from the collections, are these items disposed also.
像,
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(); }
编辑:嗯ToolStripItem的是一个例子不是一个问题,对于那些谁说清除足够我发现了另一个例子,TabControl的也有项目的收集和明确的方法。但可以的TabControls有复杂的控制(至少我),这需要明确地处置(即使他们在某个时候由GC自动处理,因为他们需要巨大的内存)。我想最好的答案是DIVO评论处置的物品,然后通话清晰。
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.
推荐答案问:确实的
答:没有的 - 清除不处置项目(他们可以在应用程序的其他部分使用)
A: No - Clear does not dispose the items (they could be used in other parts of your application).
因此,如果您ToolStripItems是标准的.NET的,应清除就足够了?一些反思后,我会说可能不会。
So, if your ToolStripItems are standard .NET ones, should Clear be sufficient? After some reflection I'd say "probably not".
是啊,这是真的,如果你在你的应用程序的其他部分的ToolStripItem的任何引用时,.NET GarbageCollector 会破坏(使用类的的析构函数的),它会自动。但是,它的不会调用的 的Dispose(真) 的方法,那就是,不过,所需形式的的IDisposable 部分。
Yeah, this is true that if you will have any references to the ToolStripItem in other part of your application, the .NET GarbageCollector will destroy(use the class destructor) it automatically. But, it will not call the Dispose(true) method, that is, however, required for the form's IDisposable components.
看了propos 这和的this 。
Read a propos this and this.
其实,我相信你会,但是,需要明确的处置您的项目,像ToolStrip的的的的Dispose 的方法并(更换这个的通过的 yourToolStrip 的):
Actually, I believe that you will, however, need to explicitly Dispose your Items, like the ToolStrip's Dispose method does (replace this by yourToolStrip):
if (!this.Items.IsReadOnly) { for (int i = this.Items.Count - 1; i >= 0; i--) { this.Items[i].Dispose(); } this.Items.Clear(); }修改
我还创建the以下螺纹,以更广泛地澄清这个问题。
EDIT
I also created the following thread to clarify this question more generally.
更多推荐
是否要求清除部署的项目也?
发布评论