title: Thread Safe C# Singleton Pattern
body: <p>I have some questions regarding the the singleton pattern as documented here:
  <a href="http://msdn.microsoft.com/en-us/library/ff650316.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/ff650316.aspx</a></p>
  
  <p>The following code is an extract from the article:</p>
  
  <pre><code>using System;
  
  public sealed class Singleton
  {
     private static volatile Singleton instance;
     private static object syncRoot = new object();
  
     private Singleton() {}
  
     public static Singleton Instance
     {
        get 
        {
           if (instance == null) 
           {
              lock (syncRoot) 
              {
                 if (instance == null) 
                    instance = new Singleton();
              }
           }
  
           return instance;
        }
     }
  }
  </code></pre>
  
  <p>Specifically, in the above example, is there a need to compare instance to null twice, before and after the lock? Is this necessary? Why not perform the lock first and make the comparison?</p>
  
  <p>Is there a problem in simplifying to the following?</p>
  
  <pre><code>   public static Singleton Instance
     {
        get 
        {
          lock (syncRoot) 
          {
             if (instance == null) 
                instance = new Singleton();
          }
  
           return instance;
        }
     }
  </code></pre>
  
  <p>Is the performing the lock expensive?</p>