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

查看:23
本文介绍了何时实施 IDisposable 的一般规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是 托管资源和本机资源在处置时有什么区别?(.NET) 和许多其他人.如果您有什么要补充的,请回答其他人.

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.

根据 Krzysztof Cwalina 和 Brad Abrams 的框架设计指南,包含一次性类型实例的类型应该实现 IDisposable.

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

对于何时实施 IDisposable 是最佳实践,还有其他一般经验法则吗?

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

推荐答案

当您有一个包装非托管资源的类或当您的类有一个实现 IDisposable 的字段时,请实现 IDisposable.

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

这篇关于何时实施 IDisposable 的一般规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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