Wednesday 15 June 2016

c# singleton

class Singleton
{
    private static Singleton instance;

    private Singleton() {}

    public static Singleton GetInstance()
    {        
            if (instance == null)
                instance = new Singleton();

            return instance;
    }
}

What does a singleton do?

It provides global access to an instance of an object, and
It guarantees that no more than one instance of that type can ever be created.
So you use a singleton when you need both of these things.

No comments:

Post a Comment