proper usage of synchronized singleton?

前端 未结 10 746
长发绾君心
长发绾君心 2020-12-31 12:09

So I am thinking about building a hobby project, one off kind of thing, just to brush up on my programming/design.

It\'s basically a multi threaded web spider, upda

10条回答
  •  清歌不尽
    2020-12-31 12:19

    Check out this article Implementing the Singleton Pattern in C#

    public sealed class Singleton
    {
        Singleton()
        {
        }
    
        public static Singleton Instance
        {
            get
            {
                return Nested.instance;
            }
        }
    
        class Nested
        {
            // Explicit static constructor to tell C# compiler
            // not to mark type as beforefieldinit
            static Nested()
            {
            }
    
            internal static readonly Singleton instance = new Singleton();
        }
    }
    

提交回复
热议问题