Singleton Pattern in C-Sharp

Singleton is a software design pattern. Which states that we can create only one instance of the class with in entire application. 

Singleton needed when one object can perform its action in whole application. 

For example We frequently use an object to connect with database. We don’t need multiple objects to create connection with database.

Here in this article we will learn, how we can create Singleton pattern step by step.

Step 1:

Create a console application in Visual Studio.

Step 2:

There is no need to use inheritance in Singleton pattern so we will create sealed class here.

public sealed class Singleton { }

Step 3:

We want to restrict this sealed class to create their objects. so we will create here an private constructor.

public sealed class Singleton { private Singleton() { } }

Step 4:

To return Instance of class we will create its property naming instance of same class type Singleton. to use it with class without its object. we will declared it static as shown below.

public sealed class Singleton { Singleton() { } private static Singleton _instance = null; public static Singleton Instance { get { if (_instance == null) { return new Singleton(); } return _instance; } } }

Note: The above is not thread-safe. Means in case of multiple threading, multiple threads will create multiple instances. which violates the singleton pattern.

So we will create an readonly object inside it to check its instance with lock statement as follows.

public sealed class Singleton { Singleton(){} private static Singleton _instance = null; private static readonly object objlock = new object(); public static Singleton Instance { get { lock (objlock) { if (_instance == null) { return new Singleton(); } } return _instance; } } }

Again above code works with poor performance as a lock is acquired every time the instance is requested.

To get rid of checking lock every time. we will add one more check here as follows.

public sealed class Singleton { Singleton(){} private static Singleton _instance = null; private static readonly object objlock = new object(); public static Singleton Instance { get { if (_instance == null) { lock (objlock) { if (_instance == null) { return new Singleton(); } } } return _instance; } } }

This implementation attempts to be thread-safe..

We can achieve it more simply by adding one more static constructor, which make ensures, when an instance of the class is created or a static member is referenced, and to execute only once per Application Domain as follows.

public sealed class Singleton { private Singleton() { } static Singleton(){} private static readonly Singleton _instance = new Singleton(); public static Singleton Instance { get { return _instance; } } }

It will be faster than adding extra checking as in the previous examples.

If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.


Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.

 
 

{{c.Content}}

Comment By: {{c.Author}}  On:   {{c.CreatedDate|date:'dd/MM/yyyy'}} / Reply


Categories