【发布时间】:2010-04-12 07:28:50
【问题描述】:
我在 this great resource 上阅读 C# 中的单例类设计,并决定使用替代方案 4:
public sealed class Singleton1
{
static readonly Singleton1 _instance = new Singleton1();
static Singleton1()
{
}
Singleton1()
{
}
public static Singleton1 Instance
{
get
{
return _instance;
}
}
}
现在我想知道这是否可以使用这样的自动属性重写?
public sealed class Singleton2
{
static Singleton2()
{
Instance = new Singleton2();
}
Singleton2()
{
}
public static Singleton2 Instance { get; private set; }
}
如果只是为了可读性,我肯定更喜欢第二个版本,但我想把它做好。
【问题讨论】:
-
编译2个版本,用Reflector反编译看看区别
标签: c# design-patterns refactoring singleton