开发者

Is my singleton implementation correct? c#

I've been reading about patterns and i am trying to implement the Singleton

Is my implementation correct? How can i Improve it? There are so many implementation on the web............

public sealed class SingletonProxy 
        {
            private static IInfusion instance;

            static SingletonProxy() { }

            SingletonProxy() { }

            public static IInfusion Instance
            {
   开发者_开发知识库             get
                {
                    if(instance == null)
                    {
                        instance = XmlRpcProxyGen.Create<IInfusion>();
                    }
                    return instance;
                }
            }
        }


... and there are so many identical questions on SO, and so many people who agree that this article provides the best solution !


There are different implementations. I often refer to Jon Skeet's good summary here:

http://www.yoda.arachsys.com/csharp/singleton.html


Since we now have the System.Lazy class, I tend to use this implementation:

public sealed class SingletonProxy
{
    private static readonly Lazy<IInfusion> instance 
          = new Lazy<IInfusion>(XmlRpcProxyGen.Create<IInfusion>);

    public static IInfusion Instance
    {
        get { return instance.Value; }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜