I am here to continue the learning series regarding Design Patterns. Today we will go through one of the popular design pattern called Singleton.
In case you have not had a look at our first article, go through the following link:
Before talking about its implementation let’s begin with some fundamental questions as in the following.
Use of the Singleton Pattern
As the name suggests, the Singleton Pattern allows only one instance of a class to be created.
When do we need to have only one instance of a class?
There are many possible requirements for a instance of a class but they all tend to have the one objective that we don’t want to change the state of the object or we want to keep the class stateless.
A simple example could be that you want to load some master data at once and let the consumers of the data make a call to the Singleton class instead of each consumer making various calls by creating a new instance.
In general, in any complex enterprise application, Repository and Data Access Layer classes can be seen as a Singleton since typically we don’t want them to maintain the state in these layers.
Some other example could be cross-cutting concerns like Logging, Configuration, Caching and so forth that can also be implemented as a Singleton since we want a single and global point of access to these classes.
Apart from the core consideration explained above, I have seen that developers, mostly not so experienced sometimes, create unnecessarily instances that creates not just an overhead to memory but also impacts the overall performance of an application.
Why not Static classes?
There can be several reasons why to not use a static class however I can think of a few as follows.
- There can be cases where you want to implement interfaces (maybe to implement IOC, I will explain IOC later) in a class that can be done in a Singleton implementation but not in the static one.
- If required, you can use a singleton class as a method parameter whereas you cannot do that with a static class.
Special care for Singleton classes
We need to take special care for Singleton classes. The idea of a state of a class comes with some extra care that means we need to handle synchronization issues in multi-threaded environments.
Enough theory, now let’s talk about implementation.
Let’s have a look at the most basic implementation.
In the first example below, we have implemented a Singleton with Lazy loading since the instance will not be created until the caller calls the GetInstance method for the first time.
- public class SingletonClass
- {
- private static SingletonClass instance = null;
- private SingletonClass() {}
- public static SingletonClass GetInstance
- {
- get
- {
- if (instance == null)
- {
- instance = new SingletonClass();
- }
- return instance;
- }
- }
- }
Let’s try to fix the sync issue that may arise in multi-threaded environments. For this, we will use a double-lock mechanism.
- public class SingletonClass
- {
- private static SingletonClass instance = null;
- private static object lockMe = new object();
- private SingletonClass() {}
- public static SingletonClass GetInstance
- {
- get
- {
- if (instance == null)
- {
- lock(lockMe)
- {
- if (instance == null)
- {
- instance = new SingletonClass();
- }
- }
- }
- return instance;
- }
- }
- }
And in the last, Singleton with static initializations. Please note that the .NET Framework guarantees thread safety for static initialization so we don’t need extra care for sync issues however we may not get the benefit of lazy loading of objects here.
- public class SingletonClass
- {
- private static SingletonClass instance = new SingletonClass();
- private SingletonClass() {}
- public static SingletonClass GetInstance
- {
- get
- {
- return instance;
- }
- }
- }
Conclusion:

Ganapati PanapanaPosted Sep 14, 2017, 2:53 PM
Nice one !! Thanks Lot!
Prakash TripathiPosted Nov 6, 2016, 12:21 PM
Not a problem Dario Benevento. Keep reading and commenting.
Dario BeneventoPosted Oct 31, 2016, 9:14 AM
Thanx a lot!
Prakash TripathiPosted Sep 5, 2016, 11:23 AM
Thnx Prasanna for reading.
Prasanna MuraliPosted Sep 5, 2016, 11:13 AM
Nice post......
Prakash TripathiPosted Jul 19, 2016, 3:55 AM
@Dario, the issue with source code file is resolved now. You may download the same (Demo.zip)
Prakash TripathiPosted Jun 18, 2016, 4:31 AM
Dario Benevento....I will reach out to the web site support team as well to fix the broken link.
Prakash TripathiPosted Jun 18, 2016, 12:34 AM
@Dario...looks like there is some issue. However the good part is that majority of code is added in the article itself so you may simply copy paste to code file and make a call to instance method from Main
Dario BeneventoPosted Jun 17, 2016, 7:59 AM
Hello! The zip file attached is not available. Thanx
Maruthi PalllamalliPosted Jun 17, 2016, 1:57 AM
if (instance == null) why should i check this for every time / call ? For more information can you please check this http://www.c-sharpcorner.com/blogs/property-based-singleton-design-pattern-in-c-sharp1
Prakash TripathiPosted Apr 17, 2016, 2:15 AM
Ok. @Aghazadeh. Even we can modify this one to accept parameter and create that number of object but that will not be singleton I believe.
Mohammad AghazadehPosted Apr 16, 2016, 3:16 PM
Hi everone, another type of singelton pattern exists with multi singelton pattern that use for create specific number of singelton object.
Mohammad AghazadehPosted Apr 16, 2016, 3:14 PM
Hi anybody,
Prakash TripathiPosted Apr 11, 2016, 3:41 AM
Thnx Humayun.
Humayun Kabir MamunPosted Apr 11, 2016, 3:37 AM
Nice...
Prakash TripathiPosted Apr 11, 2016, 2:54 AM
Thnx Ghyath.
Ghyath SerhalPosted Apr 11, 2016, 1:49 AM
Nice Article
Prakash TripathiPosted Apr 9, 2016, 10:39 AM
Thnx Nagaraj.
Kuppurasu NagarajPosted Apr 9, 2016, 10:34 AM
Nice article..
Prakash TripathiPosted Mar 11, 2016, 5:06 AM
Thnx Amit.
Amit Kumar SinghPosted Mar 11, 2016, 4:49 AM
Nice one
Prakash TripathiPosted Mar 6, 2016, 10:49 PM
Thnx Kumaresh.
Prakash TripathiPosted Mar 6, 2016, 10:48 PM
Thnx Karthiga.
Prakash TripathiPosted Mar 6, 2016, 10:48 PM
Thnx Ankur.
Prakash TripathiPosted Mar 6, 2016, 10:48 PM
Thnx Humayun.
Kumaresh RajalingamPosted Mar 6, 2016, 8:28 PM
good one
Kumaresh RajalingamPosted Mar 6, 2016, 8:27 PM
Nice explanation
Sr KarthigaPosted Mar 6, 2016, 8:16 PM
good one
Sr KarthigaPosted Mar 6, 2016, 8:16 PM
Nice explanation
Prakash TripathiPosted Dec 30, 2015, 12:51 PM
Thnx Rupesh.
Rupesh KahanePosted Dec 30, 2015, 12:40 PM
very informative for beginners..:)
Ankur MistryPosted Dec 30, 2015, 9:00 AM
Nice Share
Humayun Kabir MamunPosted Dec 14, 2015, 1:26 AM
Nice...
Prakash TripathiPosted Jul 14, 2015, 4:40 AM
Thnx Santhakumar for liking this.
Santhakumar MunuswamyPosted Jul 13, 2015, 3:08 PM
Nice article! Thanks for sharing