当我浏览下面的代码时,我找不到它在示例中使用私有构造函数的原因?
public sealed class Singleton
{
private static Singleton instance = null;
private Singleton()
{
}
public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
...
//Why shouldnt I use something like below.
public class Singleton
{
private static Singleton instance = null;
static Singleton()
{
}
public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
如果我创建了一个静态类而不是公共类,我可以直接使用该类而不是创建实例。当静态关键字持续到同一个工作时,在这里创建私有构造函数有什么需要?
遵循这种模式还有其他优势吗?