C#使用单例模式

静态:

 引自:https://jingyan.baidu.com/article/acf728fd0f85ddb9e510a3ee.html

加锁(线程安全):

public sealed class Singleton2
{
 private static Singleton2 instance = null;
 private static readonly object obj = new object();
 private Singleton2() { }
 public Singleton2 Instance
 {
  get
  {
   lock (obj)
   {
    if (instance == null)
    {
     instance = new Singleton2();
    }
    return instance;
   }
  }
 }
}
365个夜晚,我希望做到两天更一篇博客。加油,小白!
原文地址:https://www.cnblogs.com/qq2806933146xiaobai/p/15711901.html