Skip to content

Singleton code example is not thread safe #14

@isurukdniss

Description

@isurukdniss

The provided example for Singleton design pattern is not thread safe. This is the most basic implementation of the Singleton pattern. Please add the thread safe, lazy implementation.

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()
    {
    }
}

Below article from Jon Skeet would be helpful
http://csharpindepth.com/Articles/General/Singleton.aspx

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions