Creates a singleton class with System Lazy<T>

    0

    7

    lucycodes42

    C# recipes

    The preceding code declares a class named MyClass and defines a private static readonly instance of a type called Lazy<I${2}> . This instance is initialized with a new expression that creates a new instance of the type I${2} .

    Shortcut: singletonl

    public sealed class MyClass : IMyObject
    {
      private static readonly Lazy<I${2}> _instance = new Lazy<I${2}>(() => new ${1}());
    
      public static I${2} Instance { get { return _instance.Value; } }
      
      private ${1}()
      {
      }
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    Legal
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.