我应该在什么时候实现IDisposable?什么时候、IDisposable

由网友(成熟)分享简介:什么是何时实现IDisposable的最佳实践?What is the best practice for when to implement IDisposable?时的经验来实现它,如果你在类中的一个管理对象最好的规则,或者它如果对象是在类中创建或刚刚过去的依赖?我应该也都做了类,没有管理的对象?Is the...

什么是何时实现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...)

阅读全文

相关推荐

最新文章