Home  |  FAQ  |  About  |  Contact  |  View Source   
 
SEARCH:
 
BROWSE:
    My Hood
Edit My Info
View Events
Read Tutorials
Training Modules
View Presentations
Download Tools
Scan News
Get Jobs
Message Forums
School Forums
Member Directory
   
CONTRIBUTE:
    Sign me up!
Post an Event
Submit Tutorials
Upload Tools
Link News
Post Jobs
   
   
Home >  Tutorials >  C# >  Design Pattern: Singleton in C#
Add to MyHood
   Design Pattern: Singleton in C#   [ printer friendly ]
Stats
  Rating: 4.33 out of 5 by 12 users
  Submitted: 07/17/02
Pedro Silva ()

 
The Singleton design pattern is defined in the book, . This book provides implementations in C++ and Smalltalk, and this tutorial provides the code to do it in C#.

Singleton Pattern Definition
The Singleton pattern ensures that a class only has one instance and provides a global point of access to it from a well-known access point. The class implemented using this pattern is responsible for keeping track of its sole instance rather than relying on global variables to single instances of objects.

Often, this pattern is used to represent parts of the system that there can only be one of, like the file system or window manager. Or, parts of your application that there will only ever be one of.

Singleton Pattern Code
The following is the code for the implementation of the Singleton pattern:

namespace DesignPatterns
{
    /// <summary>
    /// Singleton class implements that simplest version of the Singleton 
    /// design pattern.
    /// </summary>
    public sealed class Singleton
    {
        private static readonly Singleton    instance = new Singleton();

        // make the default constructor private, so that no can directly create it.
        private Singleton()
        {
        }

        // public property that can only get the single instance of this class.
        public static Singleton Instance
        {
            get 
            {
                return instance;
            }
        }
    }
}

// code to access the Singleton.
using System.Diagnostics;
using DesignPatterns;

Trace.WriteLine(Singleton.Instance.ToString());

Singleton Code Explained
First, you'll notice that the Singleton's constructor is set to private. This ensures that no other code in your application will be able to create an instance of this class. This enforces the requirement that there ever only be one instance of this class.

Then, an instance member variable is defined of the same type as the class. This instance member is used to hold the only instance of our class. The variable is static so that it is defined only once. In the .NET Framework, static variables are defined at a point before their first use (that's the actual description of their implementation). Also, the instance variable is readonly so that it cannot be modified - not even by any code that you may write in the class's implementation.

And, a public Instance property is defined with only a get method that way callers can access the instance of this class without ever being able to change it. The property is also static to provide global access from anywhere in your program. This ensures the global point of access requirement for the Singleton.

Finally, to test the code, you can run code that accesses your Singleton class. As you can see above, it's easy enough to get at the instance of your class:
Singleton.Instance;

Place a breakpoint in instance member variable definition and another one in the Instance property get code, then run the Debugger on this code. You will notice that the member variable creation is hit just before the call to the Instance property. Also, if you place multiple calls to Singleton.Instance, you will notice that the instance member variable is only the first time, all other calls just return the cached instance variable.

And, that is the only way to get an instance of a singleton class. If you try to create an instance of the Singleton class using the new operator:
Singleton    NewInstance = new Singleton();

Than, you will get a compiler error stating that the class cannot be created because the constructor is inaccessible due to its protection level.

Thread-Safe Singleton Code
The previous code is the simplest implementation of a Singleton and can be used for most purposes. Typical WinForms applications are typically running in the UI thread, so the simple singleton will provide you with the functionality that you're looking for. However, if you are creating a multi-threaded application that needs to access a singleton across all of its threads, then you will need to create a thread-safe singleton class instead. What you gain in functionality comes at the cost of some performance, so you shouldn't use this form of the class unless you actually intend to use the class from multiple threads.

Here is the code for the thread-safe Singleton:

namespace DesignPatterns
{
    /// <summary>
    /// MTSingleton class implements the thread-safe version of the Singleton 
    /// design pattern.  This class is designed to be used to provide the singleton 
    /// across threads in a multi-threaded application.
    /// </summary>
    public sealed class MTSingleton
    {
        private static volatile MTSingleton    instance = null;
        private static object syncRoot = new object();

        // make the default constructor private, so that no can directly create it.
        private MTSingleton()
        {
        }

        // public property that can only get the single instance of this class.
        public static MTSingleton Instance
        {
            get 
            {
                // only create a new instance if one doesn't already exist.
                if (instance == null)
                {
                    // use this lock to ensure that only one thread is access
                     // this block of code at once.
                    lock(syncRoot)
                    {
                        if (instance == null)
                            instance = new MTSingleton();
                    }
                }
                // return instance where it was just created or already existed.
                return instance;
            }
        }
    }
}

// code to access the Singleton.
using System.Diagnostics;
using DesignPatterns;

Trace.WriteLine(MTSingleton.Instance.ToString());

As you can see, this class is similar in structure to the Singleton class. The instance member variable is no longer set at creation time, but in the Instance property instead. The instance variable is declared to be volatile in order to assure that the assignment of instance complete before the instance can be accessed.

The syncRoot object is used to lock on. The lock ensures that only one thread can access the instance creation code at once. That way, you won't get into a situation where two different threads are trying to create the singleton simultaneously.

And, you'll notice that we double-check the existence of the instance variable within the locked code to be sure that exactly one instance is ever created.

Finally, in your application code, access to the MTSingleton class is done in exactly the same way as with the simple singleton.

Conclusion
The Singleton design pattern has proven to be a useful pattern in many programs that I've written. Now, the code in this tutorial shows how to implement that same pattern in the .NET Framework.

Return to Browsing Tutorials

Email this Tutorial to a Friend

Rate this Content:  
low quality  1 2 3 4 5  high quality

Reader's Comments Post a Comment
 
Pedro, great tutorial! Singletons are very useful when doing things like database connection pooling. Although I have written about 15 lines of C# code in my life, I have implemented this same thing before in Java and something struck me as somewhat odd in your Multithreaded code listing. Why do you check that instance==null twice? It seems to me that the lock(syncRoot) will prevent another thread from entering the block which modifies the instance variable...effectively preventing more than one instance being created. In reviewing the lock() at MSDN, it seems like you can just replace the contents of your get block with:

lock(typeof(MTSingleton))
{
    if(instance==null)
      instance=new MTSingleton();
}
return instance;

and dump the syncRoot variable. I'm probably going to get schooled for saying this since I don't know any C# and I shouldn't be talking, but did I miss something?
-- David Behroozi, July 23, 2002
 
Probably because the lock() function is too expensive to call frequently right?
-- David Behroozi, July 23, 2002
 
I agree... It would seem silly to lock the thing everytime you ask for it just because. Unless you are modifying it then you don't really need to lock it.

You have to check twice, because once one gets the lock, another thread might try to "new up" the instance also, you don't want it to wait for the first to release the lock, and have the second thread simply create a new version.

Usually singletons have long creation times also (allocating huge chunks of memory, setting up pools of some sort, initializing tables, etc...) so such a situation is bound to happen.
-- John Gallardo, July 23, 2002
 
Yes David, you answered part of it correctly yourself. :)
The lock() operation is expensive, and you don't want to take a lock everytime you want to check if the instance exists.

And, you need the check within the lock() to ensure that no thread will enter the lock just as another one finished creating a new instance. Without that check, you would run into a situation where another instance would be created.
-- Pedro Silva, July 23, 2002
 
Good job! Singleton's are pretty useful, though I've never seen the design written in C#. As a C# beginner, it was pretty useful for some sytactical issues I hadn't seen yet. Thanks!
-- David Harris, July 23, 2002
 
This is a very well-written tutorial. You did a good job of explain the what, why, how, and when. Keep up the good work!
-- Heath Stewart, July 23, 2002
 
The "checking null twice" thing is called double-check locking. In Java, this is "broken" (there are a bunch of articles, mostly on javaworld.com if you are interested).

I was looking around and I found on MDSN regarding singletons and there are some interesting facts.

1) DCL works properly in .NET
" It turns out, and not by accident, that the Microsoft .NET Framework has addressed all of these issues, thus making it easier to implement a singleton without the adverse side effects we discussed thus far."

2) You don't have to use DCL at all, because the framework guarantees a single init for static fields.
"The Framework internally guarantees thread safety on static type initialization. In other words, in the example above, there is only one instance that would ever be created of the Singleton class."
-- Todd Ginsberg, August 28, 2002
 
This is a very well written tutorial, with information thats new and interesting to lots of readers. *****
-- Lee B, April 09, 2003
 
Thanks for explaining what a Singleton is to me. Great tutorial! *****
-- Luke Walker, April 10, 2003
 
too good an article..
-- Varkey Mathew, May 01, 2003
 
Actually, there's considerable debate about whether or not double-checked locking *does* work in .NET, with many well-respected people saying it doesn't. Fortunately, it's not needed for the singleton pattern, as has been shown before.

There are, however, a few interesting niggles in terms of laziness and the beforefieldinit flag. For those interested in it, I've written a couple of articles on the subject:




-- Jon Skeet, August 11, 2003
 
Copyright © 2001 DevHood® All Rights Reserved