Singleton

Preface

I think it is a good time to review and cleanup my C# knowledge.

Source From

Summary

  • Private constructor
  • Static instance to achieve singleton class
  • Nested class to initialized singleton class
  • Using System.Lazy is also possible way to create singleton class
public sealed class SingletonNested
{
    private SingletonNested() { }

    class SingletonCreator
    {
        static SingletonCreator() { }

        internal static readonly SingletonNested Instance = new SingletonNested();
    }

    public static SingletonNested Instance { get { return SingletonCreator.Instance; } }
}

Nomenclature

  • Assembly: An assembly is a “unit of deployment” for .NET, almost always a .exe or .dll.
  • Access modifiers: