In this article we will try to understand what Single Pattern is, what kind of problem it solves and how it should be implemented in ASP.NET.
In my last article we already talked about Design Pattern basics.
- They provide solutions to common recurring problems.
- Design patterns can be classified as creational, structural or behavioral.
What is Singleton Pattern?
- The Singleton Design Pattern ensures that only a single instance of a given object can exist at a context.
- As it solves problems related to object creation, it falls under creational pattern.
When to use Singleton Pattern?
- The Find Box in the browser or even in Visual studio (press Ctrl+F ) is the best example of a Singleton class. If you notice all the tabs share a common Find Box.
- A class that wraps the settings related to an application can be made a singleton class.
- For the On Screen Keyboard provided in the Windows operating system, we use the same keyboard object for every application.
In short, whenever we want something to be shared across multiple locations, we use a singleton pattern.
How to Create Singleton Pattern?

- First we need to restrict the user from creating instances directly.
In other words, we have to stop the user from doing something like this:

To make it possible we will make the constructor private.

- Create a member inside the ClsSingleton class of type ClsSingleton.
- private static ClsSingleton objPriSingleton=new ClsSingleton();
- Create a static method of the ClsSingleton class, GetObject, which will return the objPriSingleton.
- public static ClsSingleton GetObject()
- {
- return objPriSingleton;
- }
Note:
There are other approaches for creating a singleton class, and I just explained the one used most commonly, you can find other approaches like the static constructor approach in the source code attached.
Singleton Patterns in ASP.NET
Now let's talk about static objects, they maintain their values and reside in the memory as long as the application which contains it does.
In the singleton pattern we saw, the singleton object is marked as static.
Sharing across all users
We can use a classic approach only when we want something to be shared across all users.
Sharing across a request
In one of my previous articles about the ASP.NET life cycle we learned how an ASP.NET request works in which various modules and handlers are relevant.
When something must be shared across this request pipeline, we should replace static with HttpContext.Current.Items.
Sharing across a single user
When something must be shared across all requests related to a single user, static must be replaced with HttpContext.Current.Session.
This is how the code looks like:
- public class ClsSingleton
- {
- private ClsSingleton()
- {
- }
- public static ClsSingleton GetObject()
- {
- if(HttpContext.Current.Items["SingletonObject"] == null)
- HttpContext.Current.Items["SingletonObject"] = new ClsSingleton();
- return HttpContext.Current.Items["SingletonObject"] as ClsSingleton;
- }
- }
Please find the attached ASP.NET source code to see how a Singleton pattern is implemented in ASP.NET.
Comments are always there for queries.
Hope you enjoyed learning the Singleton pattern. In the subsequent article we will explore some patterns and see how and when to implement them in C#.
Hope to see some good comments.

Mazhar PashaPosted May 22, 2017, 6:30 AM
Hi Sukesh, If we want to create Singleton Pattern then class should be Sealed Right? In you are example its not sealed. Can you please explain me on this. And correct me if my understanding is wrong.
Sai SherlekarPosted Jun 29, 2013, 3:29 PM
Basically my question is we can do these things without this private constructor, singleton. then what is the use?
Sai SherlekarPosted Jun 29, 2013, 3:27 PM
sukesh i m not clear. can u explain me in detail. bcz to save settings across all user we can create normal class who has static property. if i want to share per user then i will store simple class object in session, and for per request no need to do anything just create simple object
Sukesh MarlaPosted Mar 8, 2013, 9:40 PM
WC, my pleasure
Shivani GuptaPosted Feb 28, 2013, 11:49 PM
Singleton with nice presentation.. thanks
Nikhil ShekharPosted Oct 31, 2012, 2:37 PM
@Sandeep - As "objPriSingleton" is a static object, hence CLR will ensure that it is created on only one thread, making it thread safe. Im not sure if the ASP.net implementation using "HttpContext.Current...." is thread safe. If you are creating an object through a method then you should use locking. Below is a good explanation of singelton pattern creation with object lock: http://msdn.microsoft.com/en-us/library/ff650316.aspx
Sandeep Singh ShekhawatPosted Sep 20, 2012, 7:41 AM
Thanks, its helpful and enjoy to reading this. But how can check that instance already exists or not in GetObject and Is it threadsafe object? can you please explain?