请给我解释一下静态构造函数的用法。为什么,什么时候我们要创建一个静态构造函数,它是否可能重载一个?


当前回答

为什么以及什么时候创建静态构造函数?

使用静态构造函数的一个特殊原因是创建一个“超级枚举”类。这里有一个(简单的,做作的)例子:

public class Animals
{
    private readonly string _description;
    private readonly string _speciesBinomialName;

    public string Description { get { return _description; } }
    public string SpeciesBinomialName { get { return _speciesBinomialName; } }

    private Animals(string description, string speciesBinomialName)
    {
        _description = description;
        _speciesBinomialName = speciesBinomialName;
    }

    private static readonly Animals _dog;
    private static readonly Animals _cat;
    private static readonly Animals _boaConstrictor;

    public static Animals Dog { get { return _dog; } }
    public static Animals Cat { get { return _cat; } }
    public static Animals BoaConstrictor { get { return _boaConstrictor; } }

    static Animals()
    {
        _dog = new Animals("Man's best friend", "Canis familiaris");
        _cat = new Animals("Small, typically furry, killer", "Felis catus");
        _boaConstrictor = new Animals("Large, heavy-bodied snake", "Boa constrictor");
    }
}

你会非常相似地(在语法外观上)使用任何其他枚举:

Animals.Dog

与常规枚举相比,它的优点是可以轻松封装相关信息。一个缺点是不能在switch语句中使用这些值(因为它需要常量值)。

其他回答

静态构造函数只调用创建的类的第一个实例。并用于执行在类的生命周期中只需要执行一次的特定操作。

来自静态构造函数(c#编程指南):

A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced. Static constructors have the following properties: A static constructor does not take access modifiers or have parameters. A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced. A static constructor cannot be called directly. The user has no control on when the static constructor is executed in the program. A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file. Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.

为什么以及什么时候创建静态构造函数?

使用静态构造函数的一个特殊原因是创建一个“超级枚举”类。这里有一个(简单的,做作的)例子:

public class Animals
{
    private readonly string _description;
    private readonly string _speciesBinomialName;

    public string Description { get { return _description; } }
    public string SpeciesBinomialName { get { return _speciesBinomialName; } }

    private Animals(string description, string speciesBinomialName)
    {
        _description = description;
        _speciesBinomialName = speciesBinomialName;
    }

    private static readonly Animals _dog;
    private static readonly Animals _cat;
    private static readonly Animals _boaConstrictor;

    public static Animals Dog { get { return _dog; } }
    public static Animals Cat { get { return _cat; } }
    public static Animals BoaConstrictor { get { return _boaConstrictor; } }

    static Animals()
    {
        _dog = new Animals("Man's best friend", "Canis familiaris");
        _cat = new Animals("Small, typically furry, killer", "Felis catus");
        _boaConstrictor = new Animals("Large, heavy-bodied snake", "Boa constrictor");
    }
}

你会非常相似地(在语法外观上)使用任何其他枚举:

Animals.Dog

与常规枚举相比,它的优点是可以轻松封装相关信息。一个缺点是不能在switch语句中使用这些值(因为它需要常量值)。

1.它只能访问类的静态成员。

原因:非静态成员特定于对象实例。如果允许静态构造函数作用于非静态成员,它将反映所有对象实例中的变化,这是不切实际的。

2.在静态构造函数中不应该有参数。

原因:因为它将被CLR调用,所以没有人可以将参数传递给它。 3.只允许一个静态构造函数。

原因:重载需要两个方法在方法/构造函数定义方面有所不同,这在静态构造函数中是不可能的。

4.它不应该有访问修饰符。

原因:同样的原因是对静态构造函数的调用是由CLR而不是由对象进行的,不需要对它有访问修饰符

不,你不能让它过载;静态构造函数对于初始化与类型相关的任何静态字段(或任何其他每种类型的操作)非常有用——特别是对于将所需的配置数据读入只读字段等非常有用。

它在第一次需要时由运行时自动运行(那里的确切规则很复杂(参见“beforefieldinit”),并且在CLR2和CLR4之间有微妙的变化)。除非滥用反射,否则它保证最多运行一次(即使两个线程同时到达)。