Inheritance and Destructors in C#

后端 未结 7 977
灰色年华
灰色年华 2021-02-13 19:30

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 m

7条回答
  •  我在风中等你
    2021-02-13 19:51

    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.

    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?

    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.

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

    That is incorrect.

    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.

    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.

    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:

    When should I create a destructor?

提交回复
热议问题