Skip to content

Instantly share code, notes, and snippets.

@msawangwan
Last active October 3, 2016 18:14
Show Gist options
  • Save msawangwan/62bb470138c290501fe37fb8f05308d7 to your computer and use it in GitHub Desktop.
Save msawangwan/62bb470138c290501fe37fb8f05308d7 to your computer and use it in GitHub Desktop.
[csharp][software design patterns] various implementations of the singleton pattern in csharp

#Implementations of the Singleton in C-Sharp

##As written by Jon Skeet

####Note:

Jon's personal preference is solution 4.

Use solution 2 over solution 4 in cases where you need to be able to call other static methods without triggering initialization or you need to know whether or not the singleton has already been instantiated.

Solution 5 is 'elegant' but trickier than 2 or 4 while solution 6 is a simpler way to achieve laziness.

Don't use solution 1 because it's broken and solution 3 is not recommended because it has no benefits over solution 5.

###1 - 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;
        }
    }
}

###2 - 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;
            }
        }
    }
}

###3 - 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;
        }
    }
}

###4 - Semi-Lazy yet Thread-Safe Without 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;
        }
    }
}

###5 - Full 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();
    }
}

###6 - Using .NET 4 Lazy 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()
    {
    }
}

####Source:

C-Sharp in Depth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment