Singleton Design Pattern in c#
Tags: softwaredesignpattern, softwarecraftmanship, csharp, dotnet
Published: 2020-01-20 14:40:37

---
title: Singleton Design Pattern in c#
published: true
description: This post is all about the Singleton Design Pattern.  It is one of the twenty-three well-known - and industry standard "Gang of Four" Design Pattern Collection.
cover_image: https://www.patrickschadler.com/wp-content/uploads/2019/08/2000px-Singleton_UML_class_diagram-e1565094559716.png
tags: Software Design Pattern,Software Craftmanship,csharp,dotnet
series: Software Design Pattern
---

# Singleton Design Pattern

This post was originally published at my personal blog [here](https://www.patrickschadler.com/singleton-design-pattern/).

With this post a brand-new series about Software Design Patterns starts. You will learn everything about the most used Design Patterns. The first is probably the most known. It is the Singleton Design Pattern. It is one of the twenty-three well-known - and industry standard - [***Gang of Four***](https://www.amazon.de/Patterns-Elements-Reusable-Object-Oriented-Software/dp/0201633612) Design Pattern Collection.

All the examples, source code and unit tests are in this [GitHub repository](https://github.com/DonkeyKongJr/SingletonDesignPattern).

{% github DonkeyKongJr/SingletonDesignPattern %}


## What's the purpose of the Singleton Design Pattern?

In software development we often instantiate a class many times. This could be problematic. As a result you have to deal with many instances of the very same class. This introduces complexity and increases memory usage of the application.

The Singleton Design Pattern solves this specific issue. It ensures that only one instance of this class exists. Beyond that it standardizes and eases the access to that instance. To access the instance, a Singleton provides per design a static `GetInstance()` method. To prevent any instantiating via the new keyword you have to hide the constructor.

## How does the Singleton Design Pattern looks like?

In the picture below you find the Unified Modeling Language (UML) diagram.

![Singleton](https://www.patrickschadler.com/wp-content/uploads/2019/08/2000px-Singleton_UML_class_diagram-e1565094559716-1024x532.png) Singleton UML Diagram

 The very basic implementation doesn't care about thread-safety. It's called [BasicSingleton](https://github.com/DonkeyKongJr/SingletonDesignPattern/blob/master/src/BasicSingleton.cs) within the GitHub Repository.

```csharp
public class BasicSingleton
{
    public string Name => Singleton;
    private static BasicSingleton instance;
    
    private BasicSingleton(){ }

    public static BasicSingleton GetInstance()
    {
        if(instance == default(BasicSingleton)){
                instance = new BasicSingleton();
        }

        return instance;
    }
}
```

## How about Thread-Safety?

This implementation could lead to problems when many threads calls the *GetInstance()* simultaneously. For example the second thread enters the if condition while the first isn't finished yet. Thus, a second instance will be created. This behavior violates the Singleton Design Pattern definition. The [ThreadSafeSingleton](https://github.com/DonkeyKongJr/SingletonDesignPattern/blob/master/src/ThreadSafeSingleton.cs) prevents those race conditions with locking the access to a thread at a time. This guarantees that only one instance will ever be created. Keep in mind that every time we access the Singleton we perform a lock which uses a bit more resources.

```csharp
public class ThreadSafeSingleton
{
    public string Name => Singleton;
    private static ThreadSafeSingleton instance;
    private static readonly object singletonlock = new object();  
    
    private ThreadSafeSingleton(){ }

    public static ThreadSafeSingleton GetInstance()
    {
        lock (singletonlock)  
        {  
            if(instance == default(ThreadSafeSingleton))
            {
                instance = new ThreadSafeSingleton();
            }

            return instance;
        }          
    }
}
```

That’s it. Happy coding as always :).