Following Singleton Design Pattern Approach in C#

Asad Iftikhar
5 min readJun 8, 2021

In this Article we are trying to achieve Singleton design Pattern Approach in C#
First we will discuss when to use Singleton Design Pattern

Advantages of Singleton Pattern

  1. Very simple to implement.
  2. The instance of the singleton class is to be created once and it remains for the lifetime of the application.

Disadvantages Of Singleton Pattern

  1. CPU time is also wasted in creation of instance if it is not required.
  2. Leads to resource wastage. Because instance of class is created always, whether it is required or not.

Create Net Core Console Application name it SingletonPattern

Then Create Another Class Library in the Solution

Add two classess in the Library

Create Two different methods in Class 1 and Class2 which we will be calling by creating Singleton Pattern Object

Method in Class2 will be like something like this

Method in Class1 will be something like this

Create three different Classes namely

  1. Class1
  2. Singleton
  3. UsingSingletonClassObjectDeclaration

Now add SingletonPattern Project Reference in SingleTonLibaray

Then Add project and Click OK

Create this Enum in your Project for Singleton to be Initialized only once for one Project

Write This Code in your Singleton Class

public sealed class Singleton{static readonly object padlock = new object();public static void Initialize(ProjectName projectName){_instance = new UsingSingleTonClassObjectDeclaration(projectName);}private Singleton(){}private static UsingSingleTonClassObjectDeclaration _instance;public static UsingSingleTonClassObjectDeclaration Instance{get{lock (padlock){if (_instance == null){throw new Exception("Library not initialized, must call 'Initialize' method first");}return _instance;}}}}

Write This Code in your UsingSingleTonClassObjectDeclaration

public class UsingSingleTonClassObjectDeclaration{public readonly Class1 class1;public readonly Class2 class2;public UsingSingleTonClassObjectDeclaration(ProjectName projectName){class1 = new Class1();class2 = new Class2();}}

Add this in your Main Console Application Program.cs

Now Your Singleton object has been Registered and you can use in another classes without creating its Object

Write This in your Class3

public void Method(){Singleton.Instance.class2.ReturnTitle();Singleton.Instance.class1.ReturnName();}

Now lets run the project and see the output details

Difference between Singleton and Static Classes : -

  1. Singleton classes can implement interfaces in Repository Pattern.

2. Singleton classes can be inherited whereas Static classes can not be inherited and can not implements interfaces.

Static class can never be Garbage collected but Singleton classes can be Garbage collected but then we can not benefits from the Singleton Repository pattern.

That’s why working with Collections and Generics are not Singleton classes but they call singleton classes.

Where to use Singleton Design Pattern.

  1. Logger file for not to create multiple logs file.

2. Creating Database one time configuration like in window service cron expressions for every 15 minutes which are not changeable.

3. Creating Db Manager class like Execute , NonExecuteQuery and other DataAdaptor methods the question arises when there is a singleton pattern then the Collection calls first will never be garbage collected but it will be because of the Reference of Singleton class from non singleton class.

4. In the above example class 3 Non singleton class is accessing Singleton classes class1 and class2 from another class library.

When not to use Singleton Design pattern .
1. When there is multithreading but can work with Parallel programming.
2. Good practice for not to use them on Db Manager when they are not in window service and database is not being called every time.

3. If not used Singleton Pattern properly will create Code-Smell which means codes architecture is not done properly and can create bigger problems at some time.

Where to use SQL Lite when there is need for temporary database means data should not be out from the private computer and we can do processing and sent it to other server like Imtiaz Reconcillation Service.
And database viewer can be Db Sqllite.

Other queries related to Singleton and static classes.

1. List.Clear() or List.DeleteAll() does not delete all the records from the list neither do they create a new object reference means if they are in Singleton object then they needs to be Garbage collected explicitly but the benefits of Singleton pattern is compromised that’s why calling from the Non Singleton Object is necessary to manipulate list.

2. Simply Static Extension ToList and other Extension methods are not Garbage collected so how the list collection be removed from them they are garbage collected because they are calls from the Object reference of non- static classes

--

--