/// <summary> /// 多線程單利模式實現 /// </summary> public class Singleton { private static volatile Singleton instance = null; private static object lockHelper = new Object(); private Singleton(){ } public static Singleton Instance { get { if(instance == null) { lock(lockHelper) { if(instance == null) { instance = new Singleton(); } } } } } }
還有一種比較簡單的單例模式,和上面功能一致c#
public class Singleton { private static Singleton instance = new Singleton(); private Singleton() { } }