C# in Depth中建议的单例模式实现是
public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();
static Singleton()
{
}
private Singleton()
{
}
public static Singleton Instance
{
get
{
return instance;
}
}
}
ReSharper建议使用auto属性和C#6自动属性初始化程序来简化它:
public sealed class Singleton
{
static Singleton()
{
}
private Singleton()
{
}
public static Singleton Instance { get; } = new Singleton();
}
这确实看起来更简单。使用这种简化有不足之处吗?