为什么不叫在C#中的所有静态构造函数(即那些父类的)? [英] Why aren't all static constructors called in C# (i.e. those of the parent classes)?

查看:212
本文介绍了为什么不叫在C#中的所有静态构造函数(即那些父类的)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个班,基本导出最后导出派生基地最后从<$ C派生$ C>派生。所有这三个类都有一个静态构造函数。类导出作为被称为公共静态方法设置。当我打电话 Final.Setup ,我希望所有三个静态构造函数得到执行,但只有一个在派生变。运行

I have three classes, Base, Derived and Final. Derived derives from Base and Final derives from Derived. All three classes have a static constructor. Class Derived as a public static method called Setup. When I call Final.Setup, I expect that all three static constructors get executed, but only the one in Derived gets run.

下面是示例源代码:

    abstract class Base
    {
        static Base()
        {
            System.Console.WriteLine ("Base");
        }
    }

    abstract class Derived : Base
    {
        static Derived()
        {
            System.Console.WriteLine ("Derived");
        }

        public static void Setup()
        {
            System.Console.WriteLine ("Setup");
        }
    }

    sealed class Final : Derived
    {
        static Final()
        {
            System.Console.WriteLine ("Final");
        }
    }

这使得只有部分道理给我。据我所知,调用 Final.Setup()其实只是为 Derived.Setup别名(),所以跳过在最后静态构造似乎合理。但是,为什么不是基本的静态构造函数叫什么名字?

This makes only partially sense to me. I understand that calling Final.Setup() is in fact just an alias for Derived.Setup(), so skipping the static constructor in Final seems fair enough. However, why isn't the static constructor of Base called?

我可以通过调用一个没有解决这个问题-operation 基本或通过访问基本的一些虚拟的静态方法静态方法。但我想知道:什么是这背后显然是奇怪的行为的理由。

I can fix this by calling into a no-operation static method of Base or by accessing some dummy static method of Base. But I was wondering: what is the reasoning behind this apparently strange behavior?

推荐答案

一个静态构造函数时(根据名为< A HREF =http://rads.stackoverflow.com/amzn/click/0321741765相对=nofollow> TCPL ):

A static constructor is called when (according to TCPL):


  • 类类型的实例被创建。

  • 任何类类型的静态成员被引用。

作为一个例子,考虑一类在开始执行静态方法:如果你有一个静态构造函数,这将是所谓的的主要方法被调用。

As an example, consider a class with the static Main method in which execution begins: if you have a static constructor, it will be called before the Main method is called.

请注意,在执行静态构造函数,甚至之前,任何静态字段都被初始化为它们的默认值,那么静态字段初始是为那些外地执行。 。只有这样,静态构造函数(cctor)被执行

Note that even before a static constructor is executed, any static fields are initialized to their default value and then the static field initializers are executed for those field. Only then, the static constructor (cctor) is executed.

要更直接地回答你的问题:静态构造函数是不可继承,他们不能直接调用,因此你的基础 cctor不会在方案中被调用,除非你给抽象的基本类的静态方法和调用第一,即在 Base.Initialize(),因为你已经建议。

To answer your question more directly: static constructors are not inherited, and they cannot be called directly, hence your Base cctor will not be called in your scenario, unless you give the abstract Base class a static method and call that first, i.e. as in Base.Initialize(), as you already suggested.

关于推理,这是简单的,以为C#(在Java中,这是不同的):静态方法是不能继承的,因此静态构造函数既不应该被继承,因为这可能会导致不必要的副作用(没事的时候引用了一类名为cctor)。

About the reasoning, that's simple, thinking C# (in Java this is different): static methods are not inherited, thus static constructors should neither be inherited as this could cause unwanted side effects (a cctor called when nothing references that class).

这篇关于为什么不叫在C#中的所有静态构造函数(即那些父类的)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆