title: How to create a perfect Singleton class in c#?
body: <p>I want to store some data with Singleton class. As far as I've studied, singleton class can be instantiated only for once. But it doesn't work for me. Can someone correct my code:  </p>
  
  <pre><code>public class MvcApplication : System.Web.HttpApplication
      {
          Singleton clientsessionidinstance = Singleton.GetInstance();
          public static void RegisterGlobalFilters(GlobalFilterCollection filters)
          {
              filters.Add(new HandleErrorAttribute());
          }
  
  
          public static void RegisterRoutes(RouteCollection routes)
          {
              routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
              routes.MapRoute(
                  "Default", // Route name
                  "{controller}/{action}/{id}", // URL with parameters
                  new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                  new string[] { "MVCPrj.Controllers" }
              );
          }
  
          protected void Application_Start()
          {
              AreaRegistration.RegisterAllAreas();
  
              RegisterGlobalFilters(GlobalFilters.Filters);
              RegisterRoutes(RouteTable.Routes);
  
              LOGICcLASSES.Logic.Auth ca = new LOGICcLASSES.Logic.Auth();
              clientsessionidinstance = Singleton.GetInstance();
              clientsessionidinstance.ClientSessionID = ca.Login(new LOGICcLASSES.Entities.ClientAuthentication()
              {
                  IP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"],
                  UserName = ConfigurationManager.AppSettings["ClientUserName"],
                  Password = ConfigurationManager.AppSettings["ClientPassword"]
              });
          }
  
          protected void Application_AcquireRequestState(object sender, EventArgs e)
          {
              if (System.Web.HttpContext.Current.Session != null)
              {
                  if (!string.IsNullOrEmpty(clientsessionidinstance.ClientSessionID))
                  {
                      CurrentUserSession.Store&lt;string&gt;("ClientSessionID", clientsessionidinstance.ClientSessionID);
                  }
              }
          }
      }  
  </code></pre>
  
  <p>So the goal is this, @ <code>Application_Start</code> I have to log in an account. Then I should save the return string on <code>HttpContext.Current.Session</code>.<br>
  Unfortunately I can't access the <code>HttpContext.Current.Session</code> inside <code>Application_Start</code> but it is possible on <code>Application_AcquireRequestState</code>.<br>
  I can use a variable that will hold the returned string and then use its value inside the <code>Application_AcquireRequestState</code>, but the big problem is this, the page loads? twice, so if I will use a variable, it will become <code>null</code> at the second load but <code>Application_Start</code> is still initiated once.<br>
  So I came up using a Singleton class, but still I get null values on second load.  </p>
  
  <p>Singleton class:  </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  namespace MVCPrj.Areas.WebPage.Logic
  {
      public class Singleton
      {
          private static Singleton instance;
  
          private Singleton() { }
  
          public static Singleton GetInstance()
          {
              lock (typeof(Singleton))
              {
                  instance = new Singleton();
              }
              return instance;
          }
  
          private string _ClientSessionID;
          public string ClientSessionID
          {
              get { return _ClientSessionID; }
              set { _ClientSessionID = value; }
          }
      }
  }  
  </code></pre>
  
  <p><strong>EDIT</strong>  </p>
  
  <p>This code works for me after reading <a href="http://geekswithblogs.net/BlackRabbitCoder/archive/2010/05/19/c-system.lazylttgt-and-the-singleton-design-pattern.aspx" rel="nofollow">Singleton</a>  </p>
  
  <pre><code>public class Singleton
  {
      private static readonly object _mutex = new object();
      private static volatile Singleton _instance = null;
      private Singleton() { }
  
      public static Singleton Instance
      {
          get
          {
              if (_instance == null)
              {
                  lock (_mutex)
                  {
                      if (_instance == null)
                      {
                          _instance = new Singleton();
                      }
                  }
              }
              return _instance;
          }
      }
  
      private string _ClientSessionID;
      public string ClientSessionID
      {
          get { return _ClientSessionID; }
          set { _ClientSessionID = value; }
      }
  }
  </code></pre>