Define private constructors to make a singleton : private « Class « C# / CSharp Tutorial






public class MyClass
{
    static MyClass cache = null;
    static object cacheLock = new object();
    private MyClass()
    {
        // useful stuff here
    }
    
    public static MyClass GetMyClass()
    {
        lock(cacheLock)
        {
            if (cache == null)
            cache = new MyClass();
            
            return(cache);
        }
    }
}








7.16.private
7.16.1.Define private constructors to make a singleton
7.16.2.Use Properties to get and set private member variable
7.16.3.private static and const Fields
7.16.4.Using Methods to change private fields