Advertisement
Hyluss

Singleton

Nov 22nd, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.83 KB | None | 0 0
  1. /*
  2. sealed class  Singleton {
  3.     private static readonly Singleton instance = new Singleton();
  4.     private Singleton() {
  5.     }
  6.     static Singleton() {
  7.     }
  8.     public static Singleton Instance {
  9.         get {
  10.             return instance;
  11.         }
  12.     }
  13. }
  14. */
  15.  
  16. public class Singleton<T> where T : class, new()
  17. {
  18.     private static readonly object syncLock = new object();
  19.     private static T instance;
  20.  
  21.     protected Singleton()
  22.     {
  23.     }
  24.  
  25.     public static T Instance
  26.     {
  27.         get
  28.         {
  29.             if (instance == null)
  30.             {
  31.                 lock (syncLock)
  32.                 {
  33.                     if (instance == null)
  34.                     {
  35.                         instance = new T();
  36.                     }
  37.                 }
  38.             }
  39.             return instance;
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement