单例模式1:
/// <summary> /// 单例 /// </summary> public sealed class Singleton { /// <summary> /// 锁 /// </summary> private static object padlock = new object(); /// <summary> /// 单例字段 /// </summary> private static Singleton instance = null; /// <summary> /// 私有构造函数 /// </summary> private Singleton() { } /// <summary> /// 实例 /// </summary> public static Singleton Instance { get { if (instance == null) { lock (padlock) { if (instance == null) { instance = new Singleton(); } } } return instance; } } }
单例模式2:
/// <summary> /// 单例 /// </summary> public sealed class Singleton { /// <summary> /// 实例 /// </summary> private static Singleton instance = new Singleton(); /// <summary> /// 私有构造函数 /// </summary> private Singleton() { } /// <summary> /// 实例 /// </summary> public static Singleton Instance { get { return instance; } } }
时间: 2024-10-09 09:20:47