I一次性链条
如果我使用 IDisposable 实现一个对象,那么拥有该对象的所有对象是否也应该实现它,即使它们没有其他资源可以释放?
If I implement a object with IDisposable, should all objects that own that object implement it as well, even if they have no other resources to release?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的。您需要处理它们,以便正确处理您的成员变量。
任何时候封装 IDisposable 类时,都应该使您的类成为 IDisposable。在您的 Dispose 方法中,您应该处置封装的资源。基本上,像对待本地资源一样对待它们。
Yes. You need to dispose of them, in order to have your member variables get disposed correctly.
Any time you encapsulate an IDisposable class, you should make your class IDisposable. In your Dispose method, you should dispose your encapsulated resources. Basically, treat them the same way you would treat a native resource.
如果您想要确定性处置,最终某些客户端需要调用 Dispose 或将调用包装在“using”块中。要渗透到您的对象,可能需要所有者也实现 IDisposable。
您不应依赖垃圾收集器来释放任何与时间相关的资源。
If you want deterministic disposal, ultimately some client needs to call Dispose or wrap the calls in a "using" block. To trickle down to your object, that might require that the owner implement IDisposable as well.
You shouldn't rely on the garbage collector to free any time-dependent resources.
是的,所属类应该实现 IDisposable,但它不需要(不应该有)Finalizer(析构函数)。
Yes, an owning class should implement IDisposable but it does not need (should not have) a Finalizer (destructor).
nop,他们只需要使用带有“using”语句的类来确保正确处置该对象,但这些对象本身不需要实现 IDisplosable
nop, they just need to use that class with a "using" statement to make sure they dispose that object properly, but those objects themselves don't need to implement IDisplosable