析构函数之间的区别,部署和落实方法 [英] Difference between destructor, dispose and finalize method

查看:135
本文介绍了析构函数之间的区别,部署和落实方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习如何垃圾收集工作在C#。我有困惑比使用析构函数处置的Finalize 方法。 根据我的研究和理解,我的类中有一个析构函数会告诉垃圾收集器进行垃圾收集在析构函数方法中提到的方式,也可以不显式调用的类的实例。 处置的方法是提供用于控制垃圾收集的用户。 Finalize方法释放被类的资源,而不是对象本身。

I am studying how garbage collector works in c#. I am having confusions over the use of Destructor, Dispose and Finalize methods. As per my research and understandings, having a Destructor method within my class will tell the garbage collector to perform the garbage collection the way mentioned in the destructor method which cannot be called explicitly on the instances of the class. Dispose method is to provide the user to control the garbage collection. Finalize method frees the resources used by the class, but not the object itself.

我不知道如果我的理解是正确的方式。请澄清疑虑。任何进一步的链接或导游的欢迎。 先谢谢了。

I am not sure if I understand it the right way. Please clarify the doubts. Any further links or guides are welcome. Thanks in advance.

推荐答案

析构函数隐式调用Finalize方法,它们在技术上是相同的。处置可与那些对象,它实现了IDisposable接口。

Destructor implicitly calls the Finalize method, they are technically same. Dispose is available with those object which implements IDisposable interface.

您可能会看到:析构函数C# - MSDN

析构函数上隐含的基类调用的Finalize   对象。

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

这是相同的链接举例:

class Car
{
    ~Car()  // destructor
    {
        // cleanup statements...
    }
}

析构函数code被隐式转换为以下code:

Destructor 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:

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.

这篇关于析构函数之间的区别,部署和落实方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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