Inheritance and Destructors in C#

2019-03-25 07:44发布

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();

When B is destroyed, its destructor wont be called?

7条回答
看我几分像从前
2楼-- · 2019-03-25 08:31

The finalizer you defined in A will be called when an instance of B is destroyed.

If you define a finalizer in both A and B, the most specific finalizer (B) will run first, then the least specific (A).

查看更多
登录 后发表回答