首页 > 代码库 > C# 单例模式

C# 单例模式

饿汉, 懒汉模式就不说了,下面是IODH模式

  static void Main(string[] args)
        {
            Singleton s1, s2;
            s1 = Singleton.getInstance();
            s2 = Singleton.getInstance();
            var obj = System.Object.Equals(s1, s2);
        }

 

 1  public class Singleton
 2     {
 3         private Singleton() { }
 4         private static class HolderClass
 5         {
 6             public static Singleton instance = new Singleton();
 7         }
 8         public static Singleton getInstance()
 9         {
10             return HolderClass.instance;
11         }
12     }

C# 单例模式