python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
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

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions