我应该在什么时候实现IDisposable? [英] When should I implement IDisposable?

查看:564
本文介绍了我应该在什么时候实现IDisposable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是何时实现IDisposable的最佳实践?

What is the best practice for when to implement IDisposable?

时的经验来实现它,如果你在类中的一个管理对象最好的规则,或者它如果对象是在类中创建或刚刚过去的依赖?我应该也都做了类,没有管理的对象?

Is the best rule of thumb to implement it if you have one managed object in the class, or does it depend if the object was created in the class or just passed in? Should I also do it for classes with no managed objects at all?

推荐答案

如果你的意思是非托管对象,然后是的,你要实现它,只要你有一个或多个非托管资源,你正在处理您的类。你也应该使用,当你可能持有的对象是自己的IDisposable模式,并确保处理掉时,你的类配置。

If you mean unmanaged objects then yes, you should be implementing it whenever you have one or more unmanaged resource you are handling in your class. You should also be using the pattern when you are possibly holding on to objects that are IDisposable themselves, and make sure to dispose of them when your class is disposed.

(同意,这个问题已经被问足够的时间来运行一个小的打印机墨水用完了,他们应该打印...)

(agreed that this question has already been asked enough times as to run a small printer out of ink should they be printed...)

这篇关于我应该在什么时候实现IDisposable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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