什么是在.NET重写Dispose(BOOL处置)的意义呢? [英] What's the point of overriding Dispose(bool disposing) in .NET?

查看:212
本文介绍了什么是在.NET重写Dispose(BOOL处置)的意义呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我写一个类在C#中实现IDisposable的,为什么不是足以让我简单地实现

If I write a class in C# that implements IDisposable, why isn't is sufficient for me to simply implement

public void Dispose(){ ... } 

要处理释放任何非托管资源?

to handle freeing any unmanaged resources?

protected virtual void Dispose(bool disposing){ ... }

总是必要的,有时是必要的,还是其他什么东西完全?

always necessary, sometimes necessary, or something else altogether?

推荐答案

这不是绝对必要的。它是推荐的一次性图案部分。如果你还没有看过这个框架设计部分(在第一版9.3,没有第二版方便抱歉),那么你应该。 <一href="http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=88e62cdf-5919-4ac7-bc33-20c06ae539ae">Try此链接。

It's not strictly necessary. It is part of the recommended Disposable pattern. If you haven't read the Framework Design Guidelines section on this (9.3 in the first edition, don't have the second edition handy sorry) then you should. Try this link.

这是很有用的一次性清理和终结区分垃圾收集-是,捣毁-我。

It's useful for distinguishing between disposable cleanup and finalizable garbage-collection-is-trashing-me.

您不必这样做的,但你应该阅读和理解为什么这个建议贴现作为不必要的了。

You don't have to do it that way but you should read up on it and understand why this is recommended before discounting it as unnecessary.

这篇关于什么是在.NET重写Dispose(BOOL处置)的意义呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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