C# 我常用的单例模式

public class TestEditor
{
    private static TestEditor instance;//如果希望多线程单便应加volatile关键词
    private static readonly object syncRoot = new object();
    private TestEditor(){}
    public static TestEditor GetInstance()
    {
         if (instance == null)
         {
             lock (syncRoot)
             {
                 if (instance == null)
                 {
                     instance = new TestEditor();
                 }
             }
         }
         return instance;
     }
}
public class SingletonTest
{
      public static readonly SingletonTest Instance = new SingletonTest();
      private SingletonTest() { }
}
public class SingletonTest
{
    public static readonly SingletonTest Instance;
    static SingletonTest()
    {
        Instance = new SingletonTest();
    }
    private SingletonTest() { }
}

 

posted @ 2024-07-05 21:03  东经115  阅读(25)  评论(0)    收藏  举报