析构、处理与定型方法的区别

我正在研究垃圾收集器在 c # 中是如何工作的。我对使用 DestructorDisposeFinalize方法感到困惑。

根据我的研究和理解,在类中使用 Destructor 方法将告诉垃圾收集器按照析构函数方法中提到的方式执行垃圾收集,该方法不能在类的实例上显式调用。

Dispose方法旨在提供用户来控制垃圾收集。Finalize方法释放类使用的资源,但不释放对象本身。

我不确定我理解的方式是否正确。请澄清疑点。欢迎任何进一步的链接或指南。

72792 次浏览

Destructor implicitly calls the Finalize method, they are technically the same. Dispose is available with objects that implement the IDisposable interface.

You may see : Destructors C# - MSDN

The destructor implicitly calls Finalize on the base class of the object.

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

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.

In C# terms, a destructor and finalizer are basically interchangeable concepts, and should be used to release unmanaged resources when a type is collected, for example external handles. It is very rare that you need to write a finalizer.

The problem with that is that GC is non-deterministic, so the Dispose() method (via IDisposable) makes it possible to support deterministic cleanup. This is unrelated to garbage collection, and allows the caller to release any resources sooner. It is also suitable for use with managed resources (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.