单例模式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/// <summary> /// 懒汉式 /// </summary> public class SingletonObjectLH { private SingletonObjectLH() { } private static readonly object lock_obj=new object(); private static SingletonObjectLH? singletonObject; public static SingletonObjectLH CreateSingletion() { if (singletonObject == null) { lock (lock_obj) { if (singletonObject == null) singletonObject = new SingletonObjectLH(); } } return singletonObject; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/// <summary> /// 饿汉式 /// </summary> public sealed class SingletonObjectEH { private static readonly SingletonObjectEH singletonObject = new SingletonObjectEH(); private SingletonObjectEH() { } public static SingletonObjectEH CreateSingletion() { return singletonObject; } }
喜欢 C#、SQL、Web