Calling of Static Constructor and Instance Constructor

前端 未结 3 1141
礼貌的吻别
礼貌的吻别 2021-02-02 03:52

As I know Constructors of parent class is called first and then child Class.But why In Case of static Constructor It executes from derived Class first and then Child Class?

3条回答
  •  轮回少年
    2021-02-02 04:41

    First off, the behaviour is not contradictory at all; it is all consistent with the rules. You just don't know what the rules are.

    You should read all of my two-part series on instance constructors and my four-part series on the semantics of static constructors. They start here:

    http://blogs.msdn.com/b/ericlippert/archive/2008/02/15/why-do-initializers-run-in-the-opposite-order-as-constructors-part-one.aspx

    and here:

    http://ericlippert.com/2013/02/06/static-constructors-part-one/

    respectively.

    Those should clearly answer your question, but in case it is not 100% clear let me sum up. The relevant rules are:

    • Rule One: A static constructor runs before any static field is accessed, before any static method is executed, and before any instance constructor is executed.
    • Rule Two: A derived-class instance constructor calls the base class instance constructor before it runs the derived class instance constructor body.

    So what happens when you execute new Child()?

    • Rule One applies. We are about to call the instance constructor of Child, so we must first call the static constructor of Child. So it runs first.
    • After the static constructor of Child returns, the instance constructor of Child runs. Rule Two applies: the first thing the Child instance constructor does before it runs its body is runs the instance constructor of Parent.
    • Rule One applies again. We are about to call the instance contructor of Parent, so we must first call the static constructor of Parent. So it runs.
    • After the static constructor of Parent returns, the instance constructor of Parent runs. Rule Two applies: it invokes the instance constructor of object, which does nothing interesting, and then runs the body of the instance constructor of Parent.
    • Control returns to the instance constructor of Child, and its body runs.

    So there you go; the order is the Child static constructor, then the Parent static constructor, then the Parent body, then the Child body.

    Now let's look at your second example. What happens when you say new XyzChild?

    • Rule One applies. We are about to call the instance constructor of XyzChild, so we first call the static constructor of XyzChild. It's body starts executing, and...
    • ...Rule One applies again. We are about to access a static field of XyzParent, so XyzParent's static constructor must execute.
    • XyzParent's static constructor executes. It accesses a field, but the static constructor is already in flight on this thread, so it does not recursively trigger the static constructor again. It prints out that it is in the parent.
    • Control returns to the child's static constructor, which prints out that it is in the child.
    • Now the child's instance constructor can run. Rule Two applies: XyzParent's instance constructor runs first.
    • Rule One applies, but the static constructor for XyzParent has already run, so it is skipped.
    • The body of XyzParent's instance constructor executes and returns control to XyzChild's static constructor.
    • The body of XyzChild's instance constructor runs.

    So there you go. There's no inconsistency whatsoever; the two rules are applied correctly.

提交回复
热议问题