何时实施IDisposable的一般规则[英] General Rule for When to Implement IDisposable

本文是小编为大家收集整理的关于何时实施IDisposable的一般规则的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

编辑:这个问题是推荐答案

当您拥有包含未管理资源的类别或类具有实现IDISPOSABLE的字段时,可以实现IDISPOSABLE.

其他推荐答案

当您需要 不受管理的资源,实施 IDisposable .

其他推荐答案

来自MSDN: http://msdn.microsoft .com/en-us/library/system.idisposable.aspx

使用此接口的处置方法与垃圾收集器结合释放非管理资源.当对象不再需要时,对象的消费者可以调用此方法.

本文地址:https://www.itbaoku.cn/post/627569.html

问题描述

EDIT: This question is a duplicate of What is the difference between managed and native resources when disposing? (.NET) and many others. Please answer the others if you have something to add.


According to the Framework Design Guidelines by Krzysztof Cwalina and Brad Abrams, a type that contains instances of disposable types should implement IDisposable.

Is there any other general rule of thumb for when it is best practice to implement IDisposable?

推荐答案

Implement IDisposable when you have a class what wraps an unmanaged resource or when your class has a field that implements IDisposable.

其他推荐答案

When you need to release unmanaged resources, implement IDisposable.

其他推荐答案

From MSDN: http://msdn.microsoft.com/en-us/library/system.idisposable.aspx

Use the Dispose method of this interface to explicitly release unmanaged resources in conjunction with the garbage collector. The consumer of an object can call this method when the object is no longer needed.