Click here to Skip to main content
13,004,099 members (114,800 online)
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 23 Apr 2008

Singleton Design Pattern - C# implementation.

, 23 Apr 2008
Rate this:
Please Sign up or sign in to vote.
This article make you aware about the singleton design pattern and its implementation in C#.


Cristopher Alexander says, “Each patterns describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice”.

Although Cristopher was an architect, who had designed great buildings, this saying holds true in our day-to-day life as well. We can very much relate this to software development. Since we come across many design problems every day, we can define a strategy for solving these problems. The strategy remains same but the implementation may vary from developer to developer.

These strategies of solving commonly occurring problem without defining the exact code are called patterns and since these are very much related to our software designs, we call then Software Design Patterns.

Singleton Pattern

The singleton pattern is a software design pattern that is used to restrict instantiation of a class to one object. This is useful when we require exactly one object of a class to perform our operations. In this pattern we ensure that the class has only one instance and we provide a global point of access to this object.

The singleton pattern can be implemented in C# by the use of static method, which provides a global point of access to a singleton object. We create a static volatile object of the class which serves as a single instance for the class. Look at the code sample given below:

public class Singleton

    // Static object of the Singleton class. 
    private static volatile Singleton _instance = null;

    /// <span class="code-SummaryComment"><summary></span>
    /// The static method to provide global access to the singleton object.
    /// <span class="code-SummaryComment"></summary></span>
    /// <span class="code-SummaryComment"><returns>Singleton object of class Singleton.</returns></span>
    public static Singleton Instance()
        if (_instance == null)
            lock (typeof(Singleton))
                _instance = new Singleton();
        return _instance;

    /// <span class="code-SummaryComment"><summary></span>
    /// The constructor is defined private in nature to restrict access.
    /// <span class="code-SummaryComment"></summary></span>
    private Singleton() { }


In the above code we have made the constructor as private, this will ensure that no one can create an object of this class using the new keyword. Then we created a static volatile object of class Singleton. This object will serve as the single point of access to this class. Going ahead we have written a public static method GetInstance(), this method is responsible for creating the static object of the Singleton class if it already doesn’t exist. If the object is already created, then this method will return an instance of the already created object. Hence the code will ensure creation of only one object of the Singleton class.

In the above code the lock statement is used for thread safety. In multi-threaded environment we would require the creation of object in a thread safe manner. Furthermore the volatile keyword is used to ensure that there is no optimizer reordering of the code.

Look at the code given below. Is it an implementation of Singleton design pattern??? This I leave it to you… explore and enjoy understanding patterns…. :)

/// <span class="code-SummaryComment"><summary></span>
/// A sealed class providing access to it's ONLY readonly static instance.
/// <span class="code-SummaryComment"></summary></span>
sealed class SingletonCounter 
    public static readonly SingletonCounter Instance = 
         new SingletonCounter();
    private SingletonCounter() {}


The Singleton design pattern is a very useful mechanism for providing a single point of object access in OOPs application. Regardless of the implementation used, the pattern provides a commonly understood concept that can be easily shared among design and development teams. A special care must be taken while implementing the singleton pattern. This pattern must only be used when required, otherwise it may act as an anti-pattern.

Wanna more Tech-Articles ?




This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sandeep Aparajit
Software Developer (Senior)
United States United States
Sandeep has 9+ yrs of IT experience. He is Microsoft Certified Technology Specialist and has been certified for Analyzing Requirements and Defining Microsoft .NET Solution Architectures.
He is an active member of:
1. MSDN Forums

My Blog
More Info

You may also be interested in...


Comments and Discussions

GeneralMy vote of 3 Pin
Peace ON20-Jul-13 1:08
memberPeace ON20-Jul-13 1:08 
GeneralMy vote of 1 Pin
Pratik.Patel26-Aug-10 20:02
memberPratik.Patel26-Aug-10 20:02 
GeneralMy vote of 1 Pin
uzunyusuf30-Dec-09 9:47
groupuzunyusuf30-Dec-09 9:47 
Questionalternate approach Pin
Pratik.Patel22-Aug-09 22:35
memberPratik.Patel22-Aug-09 22:35 
Generalsame as other articles Pin
Donsw15-Feb-09 14:54
memberDonsw15-Feb-09 14:54 
GeneralDeadlock Pin
MrTaikandi10-Jan-09 4:56
memberMrTaikandi10-Jan-09 4:56 
GeneralHere's a better article Pin
PIEBALDconsult24-Apr-08 5:47
memberPIEBALDconsult24-Apr-08 5:47 
GeneralThanks for your article but I'm confused Pin
al13n24-Apr-08 1:34
memberal13n24-Apr-08 1:34 
GeneralRe: Thanks for your article but I'm confused Pin
Joakim Rosendahl24-Apr-08 5:02
memberJoakim Rosendahl24-Apr-08 5:02 
QuestionWhats the use of the volatile modifier in your code? Pin
leppie23-Apr-08 21:49
member leppie 23-Apr-08 21:49 
AnswerRe: Whats the use of the volatile modifier in your code? Pin
johannesnestler23-Apr-08 23:59
memberjohannesnestler23-Apr-08 23:59 
AnswerRe: Whats the use of the volatile modifier in your code? Pin
Nick Butler24-Apr-08 1:51
memberNick Butler24-Apr-08 1:51 
GeneralRe: Whats the use of the volatile modifier in your code? Pin
leppie24-Apr-08 5:33
member leppie 24-Apr-08 5:33 
AnswerRe: Whats the use of the volatile modifier in your code? Pin
Nick Butler24-Apr-08 22:17
memberNick Butler24-Apr-08 22:17 
GeneralRe: Whats the use of the volatile modifier in your code? Pin
stockmonger8-Jun-11 4:49
memberstockmonger8-Jun-11 4:49 
Generaloops Pin
Nick Butler27-Apr-08 1:40
memberNick Butler27-Apr-08 1:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170627.1 | Last Updated 24 Apr 2008
Article Copyright 2008 by Sandeep Aparajit
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid