Sunday, March 21, 2010

Singleton Class in C#

Singleton pattern is very popular pattern used by programmer. The idea behind the singleton pattern is to have only one instance of a class at any time. This kind of pattern is very useful for for heavy resources like Linq Data Context etc. It is also useful in multithreaded application where different thread of application try to crate instance of one class but it should be thread safe. It can be also useful for global resources and state objects. In singleton class there are two things required. One static variable which hold the instance of class and other is a static method which will return the instance of singleton class. Here is the example of singleton class

 public class MySingleTon
{
private static MySingleTon mySingleTonInstance = new MySingleTon();

private MySingleTon()
{
//private constructor
}
public static MySingleTon GetInstace()
{
return mySingleTonInstance;
}

}
Now we are done with calling single ton class like following.
 MySingleTon objSingleTon = MySingleTon.GetInstace(); 
The above example is not thread safe so it may be possible that different thread can create different instance of classes. To make above singleton class thread safe we need to change code like following.
   public class MySingleTon
{
private static MySingleTon mySingleTonInstance = new MySingleTon();

private MySingleTon()
{
//private constructor
}
public static MySingleTon GetInstace()
{
lock (typeof(MySingleTon))
{
if (mySingleTonInstance == null)
{
mySingleTonInstance = new MySingleTon();
}
return mySingleTonInstance;
}

}
}
Hope this will help you…

Shout it
kick it on DotNetKicks.com
Share:

5 comments:

  1. That thread safe solution is not very elegant. You are needlessly locking always GetInstance() is called, and locking to typeof(MySingleTon) may cause unwanted lost of performace or even deadlocks.

    ReplyDelete
  2. Can you please suggest other way of doing this? I don't think that there will be deadlock.

    ReplyDelete
  3. Last code must be

    public class MySingleTon

    private static MySingleTon mySingleTonInstance = new MySingleTon();

    private MySingleTon()
    {
    //private constructor
    }
    public static MySingleTon GetInstace()
    {
    if (mySingleTonInstance == null)
    {
    lock (typeof(MySingleTon))
    {
    if (mySingleTonInstance == null)
    {
    mySingleTonInstance = new MySingleTon();
    }
    return mySingleTonInstance;
    }
    }
    }
    }

    ReplyDelete
  4. You can refer to msdn article http://msdn.microsoft.com/en-us/library/ms998558.aspx that explains how to implement.

    In their article the lock is only done when there is no current instance created. The instance variable is declared as "volatile"

    ReplyDelete
  5. Other people may lock on typeof(MySingleton) as well, increasing the chances of a deadlock.

    On C# (and only on it, AFAIK) you can double check safely and so make something like this:

    public static MySingleTon GetInstace()
    {
    if (mySingleTonInstance == null)
    {
    lock (lockObj)
    {
    if (mySingleTonInstance == null)
    {
    mySingleTonInstance = new MySingleTon();
    }
    }
    }
    return mySingleTonInstance;
    }

    Also, leaving the instance creation to the CLR as in:

    private static readonly MySingleTon mySingleTonInstance = new MySingleTon();

    This is already thread-safe and good enough in most occasions.

    If you want full lazy instantiation this is the way to go:

    public sealed class Singleton
    {

    private Singleton() {}

    public static Singleton GetInstance()
    {
    return NestedSingleton.singleton;
    }

    class NestedSingleton
    {
    internal static readonly Singleton singleton = new Singleton();

    static NestedSingleton() {}
    }
    }

    ReplyDelete

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews