Gigapr
Gigapr

Reputation: 319

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;
                }
            }
        }

Upvotes: 0

Views: 270

Answers (1)

jeroenh
jeroenh

Reputation: 26792

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; }
    }
}

Upvotes: 1

Related Questions