Thread Safe Singleton

  1. using System;  
  2.   
  3. public sealed class Singleton  
  4. {  
  5.    private static volatile Singleton instance;  
  6.    private static object syncRoot = new Object();  
  7.   
  8.    private Singleton() {}  
  9.   
  10.    public static Singleton Instance  
  11.    {  
  12.       get   
  13.       {  
  14.          if (instance == null)   
  15.          {  
  16.             lock (syncRoot)   
  17.             {  
  18.                if (instance == null)   
  19.                   instance = new Singleton();  
  20.             }  
  21.          }  
  22.   
  23.          return instance;  
  24.       }  
  25.    }  
  26. }