Back to: Design Patterns in C# With Real-Time Examples
Singleton Design Pattern in C# with an Example
In this article, I am going to discuss the Singleton Design Pattern in C# with Examples. Please read our previous article where we discussed Shallow Copy and Deep Copy in C# with Examples. The Singleton Design Pattern in C# falls under the Creational Pattern Category. As it belongs to the creational pattern category, it is going to be dealing with object creation and manipulation. As part of this article, we are going to discuss the following pointers.
- What is Singleton Design Pattern?
- What are the Advantages of using the Singleton Design Pattern?
- Implementation Guidelines of Singleton Pattern in C#.
- Example of the Singleton Pattern using C#.
- Some Real-time scenarios where you can use the Singleton Design Pattern.
- What are the Disadvantages of using the Singleton Design Pattern in C#?
What is Singleton Pattern in C#?
We need to use the Singleton Design Pattern in C# when we need to ensure that only one instance of a particular class is going to be created and then provide simple global access to that instance for the entire application.
As you can see in the above diagram, different clients (Client A, Client B, and Client C) trying to get the singleton instance. Once the client gets the singleton instance then they can invoke the methods (Method 1, Method 2, and Method n) using the same instance. If this is confusing at the moment then don’t worry we will discuss it with practice.
Implementation Guidelines of Singleton Design Pattern in C#:
The following are the guidelines to implement the Singleton Design Pattern in C#.
- We need to declare a constructor that should be private and parameterless. This is required because it will restrict the class to be instantiated from outside the class. It only instantiates from within the class.
- The class should be declared as sealed which will ensure that it cannot be inherited. This is going to be useful when you are dealing with the nested class. We will discuss this scenario with an example in our next article.
- We need to create a private static variable that is going to hold a reference to the singleton instance of the class.
- We also need to create a public static property/method which will return the singleton instance of the class. This method or property first checks if an instance of the singleton class is created or not. If the singleton instance is created, then it returns that singleton instance otherwise it will create an instance and then return that instance.
Example of Singleton Design Pattern using C#
Let us understand the Singleton Design pattern in C# with Examples. There are many ways, we can implement the Singleton Design Pattern in C#. They are as follows. We will discuss all the following methods to implement the Singleton Design Pattern in C#.
- No Thread-Safe Singleton Design Pattern in C#
- Thread-Safety Singleton Implementation using Lock.
- Implementing Thread-Safety Singleton Design Pattern using Double-Check Locking.
- Using Eager Loading to Implement Thread-Safety Singleton Design Pattern
- Using Lazy<T> Generic Class to Implement Lazy Loading in Singleton Design Pattern.
No Thread-Safe Singleton Design Pattern Implementation in C#
In this article, we are going to discuss the No Thread-Safe Singleton Design Pattern implementation in C#. The rest of the implementations are going to be discussed one by one in our upcoming articles. Let us understand how to implement the No Thread Safe Singleton Design Pattern in C# with an Example. First, create a class file with the name Singleton.cs and then copy and paste the following code into it. The following code is self-explained, so please go through the comment lines for a better understanding.
using System; namespace SingletonDemo { public sealed class Singleton { //This variable value will be increment by 1 each time the object of the class is created private static int Counter = 0; //This variable is going to store the Singleton Instance private static Singleton Instance = null; //The following Static Method is going to return the Singleton Instance public static Singleton GetInstance() { //If the variable instance is null, then create the Singleton instance //else return the already created singleton instance //This version is not thread safe if (Instance == null) { Instance = new Singleton(); } //Return the Singleton Instance return Instance; } //Constructor is Private means, from outside the class we cannot create an instance of this class private Singleton() { //Each Time the Constructor called, increment the Counter value by 1 Counter++; Console.WriteLine("Counter Value " + Counter.ToString()); } //The following can be accesed from outside of the class by using the Singleton Instance public void PrintDetails(string message) { Console.WriteLine(message); } } }
Here, we created the Singleton class as sealed which ensures that the class cannot be inherited from the derived classes. The class is created with a private parameterless constructor which will ensure that the class is not going to be instantiated from outside the class. But we can create instances within the same class. Again we declared the instance variable as private and also initialized it with the null value which ensures that only one instance of the class is going to be created based on the null condition. The public method GetInstance is used to return only one instance of the class by checking the value of the private variable instance. The public method PrintDetails can be invoked from outside the class through the singleton instance.
We are done with our first version of the singleton design pattern implementation which is not thread-safe. Let us see how we can use the above Singleton class in our Main method of Program class. So, modify the Main method of the Program class as follows. As you can see in the below code, we are calling the GetInstance() static method of the Singleton class which will return the Singleton instance, and then using that Singleton instance we are calling the PrintDetails. And we are doing this two times.
using System; namespace SingletonDemo { class Program { static void Main(string[] args) { //Call the GetInstance static method to get the Singleton Instance Singleton fromTeachaer = Singleton.GetInstance(); fromTeachaer.PrintDetails("From Teacher"); //Call the GetInstance static method to get the Singleton Instance Singleton fromStudent = Singleton.GetInstance(); fromStudent.PrintDetails("From Student"); Console.ReadLine(); } } }
Run the application and it will give you the following output.
As you can see in the above output, it clearly shows that the private constructor is executed only once even though we have called the GetInstance method twice and printed the counter value as 1. Hence it proves that the singleton instance is created only once. The way we implement the singleton design pattern in the above example is not to be thread-safe. It means in a multithreaded environment, it will able to create multiple instances of the singleton class. How to make the singleton class thread-safe we will discuss in our Thread-Safe Singleton Design Pattern article.
Real-Time Scenarios where we use the Singleton Design Pattern in C#:
Service Proxies: As we know invoking a Service API is an expensive operation in an application. The process that takes most of the time is creating the Service client in order to invoke the service API. If you create the Service proxy as Singleton then it will improve the performance of your application.
Facades: You can also create Database connections as Singleton which can improve the performance of the application.
Logs: In an application, performing the I/O operation on a file is an expensive operation. If you create your Logger as Singleton then it will improve the performance of the I/O operation.
Data Sharing: If you have any constant values or configuration values then you can keep these values in Singleton so that these can be read by other components of the application.
Caching: As we know fetching data from a database is a time-consuming process. In your application, you can cache the master and configuration in memory which will avoid the DB calls. In such situations, the Singleton class can be used to handle the caching with thread synchronization in an efficient manner which drastically improves the performance of the application.
What are the Advantages of using the Singleton Pattern in C#?
The Advantages of using the Singleton Design Pattern in C# are as follows.
- The first and most important advantage of using the singleton design pattern in C# is that it takes care of concurrent access to the shared resource. That means if we are sharing a resource with multiple clients simultaneously, then concurrent access to that resource is well managed by the singleton design pattern.
- It can be lazy-loaded and also has Static Initialization.
- To share common data i.e. master data and configuration data which is not changed that frequently in an application. In that case, we need to cache the objects in memory.
- As it provides a single global point of access to a particular instance, it is easy to maintain.
- To reduce the overhead of instantiating a heavy object again and again.
What are the Disadvantages of using the Singleton Design Pattern in C#?
The disadvantages of using the Singleton Design Pattern in C# are as follows:
- Unit testing is very difficult because it introduces a global state into an application.
- It reduces the potential for parallelism within a program because to access the singleton instance in a multi-threaded environment, you need to serialize the object by using locking.
In the next article, I am going to discuss Why we Need to use the Sealed Keyword in the Singleton Class as we already have a private constructor. Here, in this article, I try to explain the basic concepts of the Singleton Design Pattern in C# with Examples. I hope now you understood why we need the Singleton Design Pattern in C# with Examples.
Very nice. Easy to understand.
Excellent , explanation is very good
Very good explanation..
Very easily to understand.Thanks a lot
nice
Using “Get” in property names is not a good idea (and doesn’t meet the .NET naming standards).
You basically end up with a method called get_GetInstance !
Just call it “Instance” (or “SingleInstance” if you prefer).
Alternatively, you could make it a method: public SingleInstance GetInstance() …
Well explained. Thanks.
I guess this is not thread safe.
Yes. This is not thread-safe. We are explaining the concept step by step. Please proceed with the next article.
https://dotnettutorials.net/lesson/thread-safe-singleton-design-pattern/
easy to understand .. well explained
thank you so much for this clean and very helpful explanation