usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;namespacesingleton{ Public classSingletontest {Private StaticSingletontest Singleton; Private Static ReadOnly ObjectSyncObject =New Object(); /// <summary> ///The constructor must be private///this way, you cannot use new to create an instance of the class externally./// </summary> Privatesingletontest () {}/// <summary> ///define a global access point///set to static method///You can call the method outside of the class without instantiating it/// </summary> /// <returns></returns> Public Staticsingletontest Getsingleton () {//this is guaranteed to be instantiated only once//that is, instantiated on the first call//subsequent calls will no longer be instantiated//First heavy Singleton = = NULL if(Singleton = =NULL) { Lock(syncobject) {//Second heavy singleton = = NULL if(Singleton = =NULL) {Console.WriteLine (String.Format ("I was created by thread: {0}! ", Thread.CurrentThread.Name)); Singleton=Newsingletontest (); } } } returnSingleton; } }}
C # single case