Define private constructors to make a singleton : Private : Class C# Examples


C# Examples » Class » Private »

 

Define private constructors to make a singleton









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




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Class
» Private