C#中的继承和析构函数 [英] Inheritance and Destructors in C#

查看:234
本文介绍了C#中的继承和析构函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据,它声明无法继承析构函数或者过载。就我而言,对于所有子类,析构函数都是相同的。这几乎告诉我,我必须在每个子类中定义相同的析构函数。我无法在基类中声明析构函数并使处理具有破坏性?说我有这样的事情:

According to this, it states that Destructors cannot be inherited or overloaded. In my case, for all subclasses, the destructors will be identical. Is this pretty much telling me that I must define the same destructor in each sub class. There is no way that I can declare the destructor in the base class and have the handle the destruction? Say I have something like this:

class A
{
    ~A()
    {
        SomethingA();
    }

}

class B : A
{

}

B b = new B();

B 被销毁时,其析构函数不会被调用?

When B is destroyed, its destructor wont be called?

推荐答案


根据这一点,它声明Destructors不能被继承或重载。

According to this, it states that Destructors cannot be inherited or overloaded.

正确。析构函数不是可继承的成员,并且不是虚拟的,因此无法覆盖。它们总是具有相同的签名,因此它们不会被重载。

Correct. Destructors are not inheritable members, and are not virtual and so cannot be overridden. They always have the same signature so they cannot be overloaded.


在我的情况下,对于所有子类,析构函数将是相同的。

In my case, for all subclasses, the destructors will be identical.

您提出这样一个基本问题的事实告诉我您不应该在第一个实现析构函数正确地实现析构函数是C#中除了最简单的案例之外最困难的事情之一。为什么你认为你需要实现析构函数?

The fact that you are asking such a basic question is telling me that you should not be implementing a destructor in the first place. Implementing a destructor correctly is one of the hardest things to do in C# in all but the most trivial cases. Why do you believe that you need to implement a destructor?


这是否告诉我必须在每个子中定义相同的析构函数class?

Is this pretty much telling me that I must define the same destructor in each sub class?

不,完全没有。你是如何从析构函数不被继承的事实得出这个结论的?

No, not at all. How did you arrive at that conclusion from the fact that destructors are not inherited?


我无法在声明中声明析构函数基类和处理破坏?

There is no way that I can declare the destructor in the base class and have the handle the destruction?

当然,这是一个明智的做法,只要你坚持实施一个首先是析构函数。

Sure, that's a sensible thing to do, provided that you're bent on implementing a destructor in the first place.


当B被销毁时,它的析构函数不会被调用?

When B is destroyed, its destructor won't be called?

这是不正确的。

在我看来,你自己尝试的时间比在这里提出问题并等待回应的时间少得多。

It occurs to me that it would have taken you a lot less time to try it yourself than to ask the question here and wait for a response.


析构函数何时实际被调用?当变量超出范围时,它是否在垃圾收集中?

When does the destructors actually get called? Is it on garbage collection, when the variable falls out of scope?

我之前的推测是正确的。 在您深入了解整个垃圾收集过程之前,您绝对不应该实现析构函数。例如,您认为变量在超出范围时收集的事实表明您没有深入理解这一点,以编写正确的析构函数。

My earlier conjecture is correct. You definitely should not be implementing a destructor until you deeply understand the entire garbage collection process. The fact that you believe that variables are collected when they fall out of scope, for example, indicates that you don't understand this deeply enough to write a correct destructor.

当收集器确定某个对象无法从gc根目录中访问时,该对象的终结器没有被压缩后,通过将对象放在终结队列上以供终结器线程进行服务,将对象提升到下一代。如果没有,则回收其内存。

When an object is determined to be unreachable from a gc root by the collector, and the object has a finalizer that has not been suppressed then the object is promoted to the next generation by placing it on the finalization queue for servicing by the finalizer thread. If not, its memory is reclaimed.

当终结器线程开始运行时,它将运行该对象的所有析构函数。 (析构函数将按从大多数派生到最少派生的顺序运行。)在该过程之后,对象可能会或可能不会无法访问,并且可能会或可能不会抑制终结。如果确定该对象无法访问,那么整个过程将再次开始。

When the finalizer thread gets around to running, it runs all the destructors of the object. (Destructors will run in order from most derived to least derived.) After that process the object then may or may not be unreachable and finalization may or may not be suppressed. If the object is determined to be unreachable then the whole process starts again.

我无法强调您为了正确执行此操作而需要了解GC过程的程度。当你编写析构函数时,它会在没有意义的环境中运行。对象中的所有引用可能是仅由终结器队列生根的对象;通常所有参考都是生活。引用可能是已经完成的对象。析构函数在不同的线程上运行。即使构造函数失败,析构函数也会运行,因此对象可能甚至不能构造非原子值类型的字段可能只是部分写入 - 当一个线程被中止时,双字段完全有可能只有四个字节由构造函数设置;终结器将看到部分写入的字段。即使通过中止的事务将对象置于不一致状态,析构函数也会运行。等等。在编写析构函数时,你必须极其防御

I cannot emphasize enough how well you need to understand the GC process in order to do this correctly. When you write a destructor it runs in an environment where nothing makes sense. All the references in the object might be to objects that are only rooted by the finalizer queue; normally all references are to live things. References might be to objects that are already finalized. Destructors run on a different thread. Destructors run even if the constructor failed, so the object might not even be constructed properly. Fields of non-atomic value types may be only partially written -- it is entirely possible for a double field to have only four of its bytes set by the constructor when the thread is aborted; the finalizer will see that partially-written field. Destructors run even if the object was placed in an inconsistent state by an aborted transaction. And so on. You have to be extremely defensive when writing a destructor.

这个答案也可能有所帮助:

This answer might also help:

我应该何时创建析构函数?

这篇关于C#中的继承和析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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