简体   繁体   English

想知道为什么静态构造函数没有在派生类和基类中被击中

[英]Wondering why Static Constructor is not hit in derived class and base class

This is a little sample I was helping myself for understanding constructors working with many scenarios. 这是我帮助自己了解一些情况的构造函数的一个小样本。 I am surprised why static constructors in this scene is not being hit at all. 我很惊讶为什么这个场景中的静态构造函数根本没有被使用。

I am well aware about the Static constructors and even have experienced how they work. 我对静态构造函数非常了解,甚至还经历过它们的工作方式。 I know that, first the static constructor in the derived class gets hit and then the one in base class and then any other constructors. 我知道,首先派生类中的静态构造函数被命中,然后是基类中的静态构造函数,然后是其他任何构造函数。 But I don't know why in this particular case where I "ENFORCE" base class parameterized constructor to work, is this the reason the static constructors are not getting hit ? 但是我不知道为什么在这种特殊情况下,我的“ ENFORCE”基类参数化构造函数可以工作,这是为什么静态构造函数没有受到打击的原因吗? This is what I could suspect/understand, however I may be wrong. 这是我可能会怀疑/理解的,但是我可能是错的。 But I cannot agree with this, if this is going to be the reason. 但是,如果这将是原因,我不能同意。

Here s the code I worked in VS 2010 now: 这是我现在在VS 2010中工作的代码:

    public class MyBaseClass
    {
        public MyBaseClass(int x)
        {
        }
        static MyBaseClass()
        {
        }
    }

    public class MyDerivedClass : MyBaseClass
    {
        public MyDerivedClass(int i)
            : base(5)
        {
        }
        public MyDerivedClass() : base(2)
        {
        }
        static MyDerivedClass()
        {
        }
        public static void Main()
        {
            new MyDerivedClass();
        }
    }

I can't see why series0ne's answer is -ve marked. 我看不到为什么series0ne的答案被标记了。 It's a detailed explanation of how constructors work, with examples. 通过示例详细说明了构造函数的工作方式。

However, one thing I like to point out is that static constructors of base and derived will be called in addition to default/specific constructors by simply doing 'new B()' as the only statement. 但是,我想指出的一件事是,除了默认/特定的构造函数外,还将通过仅执行“ new B()”作为唯一语句来调用base和衍生物的静态构造函数。 You don't need to declare a variable of type A (or B for that matter) and assign to it to ensure static constructor of base is invoked. 您无需声明A类型的变量(或B的变量)并分配给它,以确保调用base的静态构造函数。 (Contradicting comment - series0ne Aug 17 '12 at 14:09) (矛盾的评论-series0ne 2012年8月17日在14:09)

Following is the an example (I wrote to verify) and its outcome: 以下是一个示例(我写过验证)及其结果:

class A
{
    static A()
    {
        Console.WriteLine("Static A.");
    }

    public A()
    {
        Console.WriteLine("Non-Static A.");
    }
}

class B : A
{
    static B()
    {
        Console.WriteLine("Static B.");
    }

    public B()
    {
        Console.WriteLine("Non-Static B.");
    }
}

void Main()
{
    new B();
}

Static B.
Static A.
Non-Static A.
Non-Static B.

EDIT - Inconsistent and incomplete testing led to an incorrect answer on my part, and with the interest of clear, concise and CORRECT answers, this post has been entirely updated. 编辑-不一致和不完整的测试导致我的答案有误,并且出于清楚,简洁和正确答案的兴趣,此帖子已完全更新。

The difference between static and non-static constructors: 静态和非静态构造函数之间的区别:

A static constructor is called automatically before the first use of an object. 在第一次使用对象之前,将自动调用静态构造函数。 It's purpose is to set up any static fields/properties/objects(etc.) of itself before any instantiation of itself occurs. 目的是在发生任何实例化之前设置其自身的任何静态字段/属性/对象(等)。 Therefore if I define class A to have a static constructor, and create an instance of A, the static constructor will execute before A is instantiated. 因此,如果我将类A定义为具有静态构造函数,并创建A的实例,则该静态构造函数将在实例化A之前执行。

Let's say for example that I construct two instances of A, I only see the static constructor for A once...why!? 例如,假设我构造了两个A实例,但我只看到一次A的静态构造函数……为什么!? Because static construction only needs to occur once for a particular object type. 因为对于特定的对象类型,静态构造只需要发生一次。 Beyond that, there is no point in performing static constructions because its already been done! 除此之外,执行静态构造是没有意义的,因为已经完成了!

Consider the following code for this exercise: 考虑一下此练习的以下代码:

    public class A
    {
        static A()
        {
            Console.WriteLine("Hello from static A");
        }

        public A()
        {
            Console.WriteLine("Hello from non-static A");
        }
    }

    public class B : A
    {
        static B()
        {
            Console.WriteLine("Hello from static B");
        }

        public B() : base() //explicit so you know B() will call A()
        {
            Console.WriteLine("Hello from non-static B");
        }
    }

Consider the following instantiation of A and it's results: 考虑以下A的实例化及其结果:

class Program
{
    static void Main(string[] args)
    {
        A instance_1 = new A();

        Console.Read();
    }
}

Results: Hello from static A, Hello from non-static A 结果: 来自静态A的Hello,来自非静态A的Hello

Since A is instantiated once in this instance, static A is called, then non-static A 由于在这种情况下实例化一次A,所以将调用静态A,然后调用非静态A

Consider the following instantiation of A (x2) and it's results: 考虑以下A(x2)的实例化及其结果:

class Program
{
    static void Main(string[] args)
    {
        A instance_1 = new A();
        A instance_2 = new A();

        Console.Read();
    }
}

Results: Hello from static A, Hello from non-static A, Hello from non-static A 结果: 来自静态A的Hello,来自非静态A的Hello,来自非静态A的Hello

As A is instantiated twice, static A is called once (as static A is a one-time operation) and non-static A is called twice to create two instances of A 当A被实例化两次时,静态A被调用一次(因为静态A是一次性操作),而非静态A被调用两次以创建A的两个实例

Consider the following instantiation of B assigned to from A and it's results: 考虑以下从A分配给B的实例化及其结果:

    class Program
    {
        static void Main(string[] args)
        {
            A instance_1 = new B();

            Console.Read();
        }
    }

Results: Hello from static B, Hello from static A, Hello from non-static A, Hello from non-static B 结果: 来自静态B的Hello,来自静态A的Hello,来自非静态A的Hello,来自非静态B的Hello

Now both A and B are being statically constructed (first time only) because B is a descendant of A. This is evident as well in that non-static B calls non-static A. as A is the ancestor or B 现在,由于B是A的后代,所以A和B都是静态构造的(仅第一次)。这也很明显,因为非静态B称为非静态A。因为A是祖先或B

Finally consider this example... 最后考虑这个例子...

class Program
{
    static void Main(string[] args)
    {
        A instance_1 = new B();
        B instance_2 = new B();

        Console.Read();
    }
}

Results: Hello from static B, Hello from static A, Hello from non-static A, Hello from non-static B, Hello from non-static A, Hello from non-static B 结果: 来自静态B的Hello,来自静态A的Hello,来自非静态A的Hello,来自非静态B的Hello,来自非静态A的Hello,来自非静态B的Hello

Again, A and B are statically constructed (one time only) but non-static A and non-static B repeat as their are now two instances of B, therefore the non-static construction occurs for every new instance of the object 同样,A和B是静态构造的(仅一次),但非静态A和非静态B重复出现,因为它们现在是B的两个实例,因此,该对象的每个新实例都会发生非静态构造

It might be worth also taking note that it appears that static construction and non-static construction calls in inheritance work the opposite way round to each other. 值得一提的是,继承中的静态构造和非静态构造调用似乎彼此相反。 IE in the last example, static B is called before static A, however non-static A is called before non-static B! 在上一个示例中,即IE,在静态A之前调用了静态B,但是在非静态B之前调用了非静态A!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM