为什么这个单例代码示例中有两个构造函数
Why two constructors in this singleton code example
我需要在代码中实现单例模式,found this version (#4)这似乎符合我的需要。
public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Singleton()
{
}
private Singleton()
{
}
public static Singleton Instance
{
get
{
return instance;
}
}
}
为什么这个例子有两个构造函数?我确实在每个构造函数中放入了 Debug.Write
并看到它们都是在 class 初始化时执行的。
我需要同时保留两者吗?哪一个更适合用于我自己的代码?
如果你有一个没有标记为 beforefieldinit
的类型,那么它的静态构造函数将在你需要它之前被调用,这意味着,如果你不使用你的单例,它就不会实例化。
更具体地说,规范指出
The semantics of when and what triggers execution of such type
initialization methods, is as follows: The semantics of when and what
triggers execution of such type initialization methods, is as follows:
- A type can have a type-initializer method, or not.
- A type can be specified as having a relaxed semantic for its type-initializer method (for convenience below, we call this relaxed
semantic BeforeFieldInit).
- If marked BeforeFieldInit then the type’s initializer method is executed at, or sometime before, first access to any static field
defined for that type.
- If not marked BeforeFieldInit then that type’s initializer method is executed at (i.e., is triggered by):
a. first access to any static field of that type, or
b. first invocation of any static method of that type, or
c. first invocation
of any instance or virtual method of that type if it is a value type
or
d. first invocation of any constructor for that type.
需要非静态构造函数,以便编译器不会创建没有参数的默认构造函数。
If a class contains no instance constructor declarations, a default instance constructor is automatically provided. That default constructor simply invokes the parameterless constructor of the direct base class. If the direct base class does not have an accessible parameterless instance constructor, a compile-time error occurs. If the class is abstract then the declared accessibility for the default constructor is protected. Otherwise, the declared accessibility for the default constructor is public. Thus, the default constructor is always of the form
静态构造函数在您的 class 第一次被调用时被调用,无论是通过像 SingleTon.MyMember
这样的静态调用还是通过创建它的新实例。另一个总是被调用,当你实例化一个新实例时(然而在单例的情况下它只有一次)。您应该将后者的访问修饰符设置为私有以避免创建实例。
编辑:如果你有一个单例 - class 你不需要两个构造函数。您的所有初始化都可以通过默认初始化完成,您可以在默认初始化中设置该实例的所有重要成员,而静态成员可以省略。
我需要在代码中实现单例模式,found this version (#4)这似乎符合我的需要。
public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Singleton()
{
}
private Singleton()
{
}
public static Singleton Instance
{
get
{
return instance;
}
}
}
为什么这个例子有两个构造函数?我确实在每个构造函数中放入了 Debug.Write
并看到它们都是在 class 初始化时执行的。
我需要同时保留两者吗?哪一个更适合用于我自己的代码?
如果你有一个没有标记为 beforefieldinit
的类型,那么它的静态构造函数将在你需要它之前被调用,这意味着,如果你不使用你的单例,它就不会实例化。
更具体地说,规范指出
The semantics of when and what triggers execution of such type initialization methods, is as follows: The semantics of when and what triggers execution of such type initialization methods, is as follows:
- A type can have a type-initializer method, or not.
- A type can be specified as having a relaxed semantic for its type-initializer method (for convenience below, we call this relaxed semantic BeforeFieldInit).
- If marked BeforeFieldInit then the type’s initializer method is executed at, or sometime before, first access to any static field defined for that type.
- If not marked BeforeFieldInit then that type’s initializer method is executed at (i.e., is triggered by):
a. first access to any static field of that type, or
b. first invocation of any static method of that type, or
c. first invocation of any instance or virtual method of that type if it is a value type or
d. first invocation of any constructor for that type.
需要非静态构造函数,以便编译器不会创建没有参数的默认构造函数。
If a class contains no instance constructor declarations, a default instance constructor is automatically provided. That default constructor simply invokes the parameterless constructor of the direct base class. If the direct base class does not have an accessible parameterless instance constructor, a compile-time error occurs. If the class is abstract then the declared accessibility for the default constructor is protected. Otherwise, the declared accessibility for the default constructor is public. Thus, the default constructor is always of the form
静态构造函数在您的 class 第一次被调用时被调用,无论是通过像 SingleTon.MyMember
这样的静态调用还是通过创建它的新实例。另一个总是被调用,当你实例化一个新实例时(然而在单例的情况下它只有一次)。您应该将后者的访问修饰符设置为私有以避免创建实例。
编辑:如果你有一个单例 - class 你不需要两个构造函数。您的所有初始化都可以通过默认初始化完成,您可以在默认初始化中设置该实例的所有重要成员,而静态成员可以省略。