public sealed class Singleton<T> where T:new()
...{
private static T instance = new T();
private static object lockHelper = new object();
private Singleton() ...{ }
public static T GetInstance()
...{
if (instance == null)
...{
lock (lockHelper)
...{
if (instance == null)
...{
instance = new T();
}
}
}
return instance;
}
}

本文介绍了一种使用泛型和双重检查锁定的线程安全的C# Singleton模式实现方法。该实现利用静态构造函数和锁对象确保实例唯一且延迟加载。
1541

被折叠的 条评论
为什么被折叠?



