質問

Why var b = new B() firstly enters static B() .ctor and than static A() .ctor and not vice versa like the instance constructors does (public A() and than public B())?

public class A
{
    static A() {}
    public A() {}
}

public class B : A
{
    static B() {}
    public B() {}
}
役に立ちましたか?

解決

Technically the instance constructor of B is entered first. But the first thing it does is calling the constructor of A and only then goes to the user defined body.

So I assume that directly before the constructor of B is entered the static constructor of B needs to run.

Then the constructor of B calls the constructor of A, which triggers the static constructor of A.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top