Design Pattern Concepts

Singleton Design Pattern in C#

What Is Singleton Pattern?

The Singleton Pattern ensures that a class has only one instance in the across application and the object instance coordinate to across the app.

This pattern is the simplest design patterns and used to restrict the creation of multiple objects instance.
For more detail kindly refer the link - /design-pattern-concepts-mvc-pattern.html

This pattern is the simplest design patterns and used to restrict the creation of multiple objects instance.

Table of Contents - Implementing the Singleton Pattern in C#
1.      Introduction - What Is Singleton Pattern?
2.      Non-thread-safe version
3.      Simple thread safety via locking
4.      Double-checked locking
5.      Safety through initialization
6.      Safe and fully lazy static initialization

7.      Lazy<T>

Lest see the example for implementing the Singleton Pattern in C# in depth –
//First version - not thread-safe
// Bad code! Do not use!
public sealed class Singleton
{
    private static Singleton instance=null;
    private Singleton() {   }
    public static Singleton Instance
    {
        get
        {
            if (instance==null)
            {
                instance = new Singleton();
            }
            return instance;
        }
    }
}
Example 2 –
//Second version - simple thread-safety
public sealed class Singleton
{
    private static Singleton instance = null;
    private static readonly object padlock = new object();
    Singleton()  { }
    public static Singleton Instance
    {
        get
        {
            lock (padlock)
            {
                if (instance == null)
                {
                    instance = new Singleton();
                }
                return instance;
            }
        }
    }
}
Example 3-
//Third version - attempted thread-safety using double-check locking
// Bad code! Do not use!
public sealed class Singleton
{
    private static Singleton instance = null;
    private static readonly object padlock = new object();
    Singleton() {  }
    public static Singleton Instance
    {
        get
        {
            if (instance == null)
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new Singleton();
                    }
                }
            }
            return instance;
        }
    }
}
Example 4 –
//Fourth version - not quite as lazy, but thread-safe without using locks
public sealed class Singleton
{
    private static readonly Singleton instance = new Singleton();
    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit
    static Singleton() {  }
    private Singleton() {  }
    public static Singleton Instance
    {
        get
        {
            return instance;
        }
    }
}
Example 5-
//Fifth version - fully lazy instantiation
public sealed class Singleton
{
    private Singleton()  {  }
    public static Singleton Instance { get { return Nested.instance; } }       
    private class Nested
    {
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static Nested()
        {
        }
        internal static readonly Singleton instance = new Singleton();
    }
}
Example 6 –
If you are using .NET 4 (or higher), you should use the System.Lazy<T> type to make the laziness really simple -
//Sixth version - using .NET 4's Lazy<T> type
public sealed class Singleton
{
    private static readonly Lazy<Singleton> lazy =
        new Lazy<Singleton>(() => new Singleton());
   
    public static Singleton Instance { get { return lazy.Value; } }
    private Singleton() {  }
}


ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

www.code-sample.com/. Powered by Blogger.
^