清除之前,是否需要将项目放置在C#列表中? [英] Do I need to Dispose items in a c# list before clearing it?

查看:146
本文介绍了清除之前,是否需要将项目放置在C#列表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自list.Clear()

Count设置为0,并且还会释放对集合元素中其他对象的引用.

Count is set to 0, and references to other objects from elements of the collection are also released.

根据我所教的内容(可能是错误的),请处置&释放是不同的东西.丢弃表示已将项目从内存中完全删除,释放则意味着未通过指针将其绑定到该列表.

From what I've been taught (which could be wrong), disposed & released are different things. Disposed means the items is completely removed from memory, released means it's simply not bound to that list by pointer.

这是否意味着我需要做

foreach (var item in Items)
{
    item.Dispose();
}
this.Items.Clear();

如果我实际上想彻底销毁/清除/释放/销毁存在的名单?

If I effectively want to completely destroy/clear/release/dipose a list from existence?

对我来说,一切都应该像.Clear()一样简单,但是,对于我(还是双关语意)尚不清楚,是否足够且正确.

To me it should all be as simple as .Clear(), but, it's a bit unclear to me (pun intended) if that is enough and correct.

请注意,我们不会覆盖Dispose()Clear(),它们都是默认实现.

Note that we are not overriding Dispose() nor Clear(), it's all default implementation.

此外,列表是List<T>.

根据评论的建议,我已经去检查了,所有项目都是IDisposables,这使人们对应该将其丢弃的事实有所了解

Edit : As per comment recommendation, I've went and checked, the items are all IDisposables, which sheds some light on the fact that they should be disposed

> IDisposable的用途上有重复的说明? :

Duplicate clarification on What is IDisposable for? :

我不认为这两个问题是相同的,我是在问两件事之间的区别,另一个是在澄清其中一件事.在我决定写这个问题之前,它也没有出现在我的搜索中,因为我太关注差异处理与清除"之类的关键字,将来的开发人员在寻找答案时也可能会遇到这种情况.我承认另一个答案提供了更多信息,并且是在此处添加的一个很好的链接.

I do not think these two questions are the same, mine is asking a difference between two things, the other is asking for clarification on one of those things. It also did not appear in my search before I decided to write the question, because I was too focused on keywords like "difference dispose vs clear", which will probably also be the case for future developers looking for an answer. I'll concede that the other answer provides some more information and is a good link to add here.

推荐答案

根据我所教的内容(可能是错误的),请处置&释放的是不同的东西.

From what I've been taught (which could be wrong), disposed & released are different things.

是.

Dispose表示已将项目从内存中完全删除,release表示未通过指针将其绑定到该列表.

Disposed means the items is completely removed from memory, released means it's simply not bound to that list by pointer.

错了.处置意味着对与托管内存无关的对象进行的所有清理都已完成.

Wrong. Disposed means that any clean-up required by an object that has nothing to do with managed memory is done.

如果元素是IDisposable,那么在清除之前将它们全部Dispose()确实是一个好主意,如果通常通过该列表可以到达它们,因此没有其他内容会Dispose().

If the elements are IDisposable then it is indeed a good idea to Dispose() them all before clearing, if they are generally reached through that list and hence nothing else will Dispose() them.

如果不是IDisposable,则不仅不需要,而且也是不可能的.

If they aren't IDisposable this not only isn't needed, but it's not possible.

如果它们是IDisposable,但是其他元素仍会使用,那么您不应该Dispose(),因为它们会破坏其他用途.

If they are IDisposable but something else will still be using the elements, then you shouldn't Dispose() them as that will break that other use.

这篇关于清除之前,是否需要将项目放置在C#列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆