Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Simple Singleton Pattern in C#

3.00/5 (2 votes)
27 Oct 2011CPOL 13.3K  
Another alternative: class Singleton { public static Singleton m_Instance; //Prevent instance creation from other classes private Singleton() { } public static Singleton Instance { get { return m_Instance ?? (m_Instance = new Singleton()); } ...
Another alternative:

C#
class Singleton 
{
    public static Singleton m_Instance;
 
    //Prevent instance creation from other classes
    private Singleton() { }
 
    public static Singleton Instance 
    { 
       get { return m_Instance ?? (m_Instance = new Singleton()); } 
    }
} 

Could you comment pros and cons with alternative-3?

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)