Click here to Skip to main content
13,290,261 members (52,461 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


35 bookmarked
Posted 6 Jul 2011

Simple Singleton Pattern in C#

, 6 Jul 2011
Rate this:
Please Sign up or sign in to vote.
A brief explaination of how to implement the Singleton pattern class in multithreading environment.
Often, a system only needs to create one instance of a class, and that instance will be accessed throughout the program. Examples would include objects needed for logging, communication, database access, etc.

So, if a system only needs one instance of a class, and that instance needs to be accessible in many different parts of a system, one control both instantiation and access by making that class a singleton.

A Singleton is the combination of two essential properties:

Ensure a class only has one instance.
Provide a global point of access to it.

You can find many articles on SingleTon Pattern but this article will give you an insight about the basics on SingleTon Pattern especially in the case of multithreading environment.

As stated above, a singleton is a class that can be instantiated once, and only once.

To achieve this we need to keep the following things in our mind.

1. Create a public Class (name SingleTonSample).

public class SingleTonSample

2. Define its constructor as private.

private SingleTonSample()

3. Create a private static instance of the class (name singleTonObject).

private volatile static SingleTonSample singleTonObject;

4. Now write a static method (name InstanceCreation) which will be used to create an instance of this class and return it to the calling method.

public static SingleTonSample InstanceCreation()
    private static object lockingObject = new object();
    if(singleTonObject == null)
         lock (lockingObject)
              if(singleTonObject == null)
                   singleTonObject = new SingleTonSample();
    return singleTonObject;

Now we to need to analyze this method in depth. We have created an instance of object named lockingObject, its role is to allow only one thread to access the code nested within the lock block at a time. So once a thread enter the lock area, other threads need to wait until the locking object get released so, even if multiple threads try to access this method and want to create an object simultaneously, it's not possible. Further only if the static instance of the class is null, a new instance of the class is allowed to be created.

Hence only one thread can create an instance of this Class because once an instance of this class is created the condition of singleTonObject being null is always false and therefore rest all instance will contain value null.

5. Create a public method in this class, for example I am creating a method to display message (name DisplayMessage), you can perform your actual task over here.

public void DisplayMessage()
     Console.WriteLine("My First SingleTon Program");

6. Now we will create an another Class (name Program).

class Program

7. Create an entry point to the above class by having a method name Main.

static void Main(string[] args)
    SingleTonSample singleton = SingleTonSample.InstanceCreation();

Now we to need to analyse this method in depth. As we have created an instance singleton of the class SingleTonSample by calling the static method SingleTonSample.InstanceCreation() a new object gets created and hence further calling the method singleton.DisplayMessage() would give an output "My First SingleTon Program".


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


About the Author

Shashank Bisen
Software Developer (Senior) 1E
India India
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionLearn more on Pin
Mayank_Gupta_30-Nov-16 0:56
professionalMayank_Gupta_30-Nov-16 0:56 
PraiseShort & Clear Pin
pamoja559-May-16 22:25
memberpamoja559-May-16 22:25 
QuestionNot allowed to declare specifiers in member function Pin
Mohan_Patil31-May-15 20:26
professionalMohan_Patil31-May-15 20:26 
GeneralFine Pin
victowork2-Nov-14 23:51
membervictowork2-Nov-14 23:51 
GeneralRated Excellent :) Pin
satheesh.palanisamy1-Dec-13 4:27
membersatheesh.palanisamy1-Dec-13 4:27 
QuestionMy vote of 4 Pin
Peace ON20-Jul-13 1:50
memberPeace ON20-Jul-13 1:50 
GeneralMy vote of 5 Pin
amirhosseineniac14-Mar-13 5:05
memberamirhosseineniac14-Mar-13 5:05 
QuestionThanks Pin
VikramBansod9-Dec-12 8:22
memberVikramBansod9-Dec-12 8:22 
AnswerRe: Thanks Pin
♥…ЯҠ…♥19-Mar-14 22:13
professional♥…ЯҠ…♥19-Mar-14 22:13 
GeneralAlternative 8 ends all discussion. Alternative 7 provides a ... Pin
Kabwla.Phone6-Nov-11 22:30
memberKabwla.Phone6-Nov-11 22:30 
GeneralI don't understand how do you asure that this's ThreadSafe. ... Pin
LuchoStudio1-Nov-11 4:30
memberLuchoStudio1-Nov-11 4:30 
GeneralReason for my vote of 1 Not the recommended way to implement... Pin
Richard Deeming13-Jul-11 9:15
memberRichard Deeming13-Jul-11 9:15 
GeneralLazy<T> is the Microsoft implementation of Singleton pattern... Pin
Mohammad A Rahman6-Jul-11 15:12
memberMohammad A Rahman6-Jul-11 15:12 
GeneralReason for my vote of 1 As Piebald says: The lock is wrong, ... Pin
Erich Ledesma4-Jul-11 11:46
memberErich Ledesma4-Jul-11 11:46 

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
Web04 | 2.8.171207.1 | Last Updated 6 Jul 2011
Article Copyright 2011 by Shashank Bisen
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid