本文介绍了为什么的Dispose()是不虚?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C#,所以道歉,如果这是一个明显的问题。

I'm new to C#, so apologies if this is an obvious question.

在href="http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx"> MSDN处置例如的

In the MSDN Dispose example, the Dispose method they define is non-virtual. Why is that? It seems odd to me - I'd expect that a child class of an IDisposable that had its own non-managed resources would just override Dispose and call base.Dispose() at the bottom of their own method.

谢谢!

推荐答案

典型用法是,Dispose()方法被重载,与公共,非虚拟的Dispose()方法,以及一个虚拟的,受保护的Dispose(布尔)。公共Dispose()方法调用Dispose(真),和子类可以使用该受保护的虚拟方法来释放自己的resorces,并调用base.Dispose(真)的父类。

Typical usage is that Dispose() is overloaded, with a public, non-virtual Dispose() method, and a virtual, protected Dispose(bool). The public Dispose() method calls Dispose(true), and subclasses can use this protected virtual method to free up their own resorces, and call base.Dispose(true) for parent classes.

如果该类拥有公众Dispose()方法还实现一个终结,则终结调用Dispose(假),这表明被保护的Dispose(布尔)方法被垃圾收集期间调用

If the class owning the public Dispose() method also implements a finalizer, then the finalizer calls Dispose(false), indicating that the protected Dispose(bool) method was called during garbage collection.

如果有一个终结,那么公众的Dispose()方法还负责调用GC.Sup pressFinalize(),以确保终结不再处于活动状态,并且永远不会被调用。这使得垃圾收集器一般治疗类。与活跃的终结类一般得到收集只能作为最后的手段,gen0,第一代,第二代和清理后。

If there is a finalizer, then the public Dispose() method is also responsible for calling GC.SuppressFinalize() to make sure that the finalizer is no longer active, and will never be called. This allows the garbage collector to treat the class normally. Classes with active finalizers generally get collected only as a last resort, after gen0, gen1, and gen2 cleanup.

这篇关于为什么的Dispose()是不虚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 23:45