C# 析构函数、处置和终结方法之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13988334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between destructor, dispose and finalize method
提问by Victor Mukherjee
I am studying how garbage collector works in c#. I am confused over the use of Destructor
, Dispose
and Finalize
methods.
我正在研究垃圾收集器在 C# 中的工作原理。我对Destructor
,Dispose
和Finalize
方法的使用感到困惑。
As per my research and understandings, having a Destructor method within my class will tell the garbage collector to perform the garbage collection in the way mentioned in the destructor method which cannot be called explicitly on the instances of the class.
根据我的研究和理解,在我的类中有一个 Destructor 方法将告诉垃圾收集器以析构函数方法中提到的方式执行垃圾收集,而不能在类的实例上显式调用。
The Dispose
method is meant to provide the user to control the garbage collection. The Finalize
method frees the resources used by the class, but not the object itself.
该Dispose
方法旨在为用户提供控制垃圾收集的功能。该Finalize
方法释放类使用的资源,但不释放对象本身。
I am not sure if I understand it the right way. Please clarify the doubts. Any further links or guides are welcome.
我不确定我是否理解正确。请澄清疑惑。欢迎提供任何进一步的链接或指南。
采纳答案by Habib
Destructor implicitly calls the Finalize method, they are technically the same. Dispose is available with objects that implement the IDisposable interface.
析构函数隐式调用 Finalize 方法,它们在技术上是相同的。Dispose 可用于实现 IDisposable 接口的对象。
You may see : Destructors C# - MSDN
您可能会看到:析构函数 C# - MSDN
The destructor implicitly calls Finalize on the base class of the object.
析构函数在对象的基类上隐式调用 Finalize。
Example from the same link:
来自同一链接的示例:
class Car
{
~Car() // destructor
{
// cleanup statements...
}
}
The Destructor's code is implicitly translated to the following code:
析构函数的代码被隐式转换为以下代码:
protected override void Finalize()
{
try
{
// Cleanup statements...
}
finally
{
base.Finalize();
}
}
Your understanding for the Destructor is right:
您对析构函数的理解是正确的:
From MSDN
来自MSDN
The programmer has no control over when the destructor is called because this is determined by the garbage collector. The garbage collector checks for objects that are no longer being used by the application. If it considers an object eligible for destruction, it calls the destructor (if any) and reclaims the memory used to store the object. Destructors are also called when the program exits. It is possible to force garbage collection by calling Collect, but most of the time, this should be avoided because it may create performance issues.
的程序员时,因为这是由垃圾收集器确定的析构函数被称为无法控制。垃圾收集器检查应用程序不再使用的对象。如果它认为一个对象符合销毁条件,它会调用析构函数(如果有)并回收用于存储该对象的内存。程序退出时也会调用析构函数。可以通过调用 Collect 来强制垃圾回收,但大多数情况下,应该避免这样做,因为它可能会产生性能问题。
回答by Marc Gravell
In C# terms, a destructor and finalizer are basically interchangeable concepts, and should be used to release unmanagedresources when a type is collected, for example external handles. It is veryrare that you need to write a finalizer.
在 C# 术语中,析构函数和终结器基本上是可互换的概念,应该用于在收集类型时释放非托管资源,例如外部句柄。这是非常罕见的,你需要写一个终结。
The problem with that is that GC is non-deterministic, so the Dispose()
method (via IDisposable
) makes it possible to support deterministiccleanup. This is unrelated to garbage collection, and allows the caller to release any resources sooner. It is also suitable for use with managedresources (in addition to unmanaged), for example if you have a type that encapsulates(say) a database connection, you might want disposing of the type to release the connection too.
问题在于 GC 是非确定性的,因此Dispose()
方法 (via IDisposable
) 可以支持确定性清理。这与垃圾收集无关,并允许调用者更快地释放任何资源。它也适用于托管资源(除了非托管资源),例如,如果您有一个封装(比如)数据库连接的类型,您可能也希望处理该类型以释放连接。