是否要求清除部署的项目也?

编程入门 行业动态 更新时间:2024-10-07 00:21:07
本文介绍了是否要求清除部署的项目也?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

很多时候,有一个明显的方法,可去除集合中的所有项目,在这些项目处置也。

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.

更多推荐

是否要求清除部署的项目也?

本文发布于:2023-11-28 03:33:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640796.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!