IDisposable接口并实现解构方法
我正在研究 C# 理论,但我对理论的某些部分不太清楚。当我必须在类中调用析构函数方法时,我必须实现 IDisposable 接口?我的意思是实现接口与析构函数严格相关?
I'm studying C# theory and I don't have some part of theory clear. When I have to call in a class the destructor method I MUST implement IDisposable interface?I mean implementing interface is strictly correlated with destructor ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
C# 中的“析构函数”不会“销毁”对象。相反,它指示编译器生成称为“终结器”的东西,它的存在向垃圾收集器表明,当发现对象已被放弃时,应该通知该事实(通过运行主体中的代码)析构函数)在其或其直接或间接引用的任何对象被销毁之前。
对象很少需要做一些事情来响应被遗弃的情况。如果一个对象已请求一个或多个外部实体代表其执行某些操作,直至另行通知(例如,它已请求并被授予对文件的独占访问权限),则应在不再需要这些实体的服务时立即通知这些实体。最好的方法是让对象实现 IDisposable.Dispose,并使用一种方法来实现,该方法将通过执行此类通知来整理其事务,并让此类对象的用户在它不再存在时通知它。需要时,在放弃之前,通过调用前述的
IDisposable.Dispose
。终结器通常仅作为后退程序存在,以防对象被错误地放弃,而没有首先通知它们不再需要它们。从教学的角度来看,我会推迟教授有关析构函数的任何内容,除了这样一个事实:存在一些方法可以使那些在没有首先调用 IDisposable.Dispose 的情况下错误地放弃对象的程序能够“有点”工作。如果析构函数/终结器编写得不正确,通常会将原本以清晰明显的方式失败的程序变成通常可以运行的程序,但偶尔会以奇怪和莫名其妙的方式出现故障。想想看,即使正确编写的析构函数/终结器也可以产生大致相同的效果,尽管失败的频率较低且不那么奇怪。恕我直言,新手学习如何编写终结器所花费的任何努力最好花在学习如何不需要它们上。
A "destructor" in C# does not "destroy" an object. To the contrary, it directs the compiler to generate something called a "finalizer", whose existence indicates to the garbage collector that when an object is found to have been abandoned, it should be notified of that fact (by running the code within the body of the destructor) before it, or any object to which it holds a direct or indirect reference, is destroyed.
Rarely should there be any need for an object to do something in response to being abandoned. If an object has requested one or more outside entities to do something on its behalf until further notice (e.g. it has requested and been granted exclusive access to a file) it should notify such entities as soon as their services are no longer needed. This is best accomplished by having the object implement
IDisposable.Dispose
with a method that will put its affairs in order by carrying out such notifications, and by having the user of such object notify it when it is no longer needed, before abandoning it, by calling the aforementionedIDisposable.Dispose
. Finalizers generally only exist as a back-stop in case objects get wrongfully abandoned without having been first notified that they were no longer needed.From a pedagogical standpoint, I would defer teaching anything about destructors beyond the fact that there exist ways by which programs which wrongfully abandon objects without first calling
IDisposable.Dispose
can be made to 'kinda sorta' work. Having an improperly written destructors/finalizers will often turn a program which would have failed in a clear and obvious fashion into one which usually works, but occasionally malfunctions in bizarre and inexplicable ways. Come to think of it, even properly written destructors/finalizers can have much the same effect, though with the failures being less frequent and less bizarre. Any efforts spent by a novice learning how to write finalizers would IMHO be better spent learning how not to need them.不,如果您在 C# 类中实现“终结器”,则不需要同时实现 IDisposable 接口。然而,这样做被认为是“最佳实践”。
为什么?那么,在类中实现终结器的典型原因是回收类实例“拥有”的稀缺资源。也许该类管理数据库连接或文件或资源句柄。由于 .NET 使用垃圾收集来管理对象生命周期,因此无法保证终结器何时运行(有一些方法可以强制终结器运行,但这样做有明显的缺点)。
IDisposable 接口为类的使用者提供了一种更仔细地控制由该类管理的资源的使用的方法。当您实现 IDisposable 接口时,您可以将终结器视为在类未正确“处置”时的故障安全机制。
No, if you implement a 'finalizer' in your C# class, you are not required to also implement the IDisposable interface. It is, however, considered to be a "best practice" to do so.
Why? Well, the typical reason to implement a finalizer in a class is to reclaim scarce resources 'owned' by instances of the class. Perhaps the class manages a database connection or a file or resource handle. Since .NET uses garbage collection to manage object lifetimes, there is no guarantee as to when a finalizer will be run (there are ways to force finalizers to run, but there are definite drawbacks to doing this).
The IDisposable interfaces provides a way for consumers of a class to more carefully control the use of the resource(s) managed by the class. When you implement the IDisposable interface, you can consider a finalizer to be a fail-safe mechanism in the event the class is not 'disposed' properly.