单列模式的类 class Singleton { private static Singleton instance; private Singleton() { } public static Singleton Instance { get { if (instance == null) { instance = new Singleton(); } return instance; } } }
通过以下代码,进行测试,是不是同一个实例
Singleton demo = Singleton.Instance; Singleton demo2 = Singleton.Instance; Console.Write(object.ReferenceEquals(demo,demo2)); Console.Read();
输出:true,显然是同一个实例
时间: 2024-11-08 01:34:25