65.9K
CodeProject is changing. Read more.
Home

Simple Singleton Pattern in C#

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (2 votes)

Oct 27, 2011

CPOL
viewsIcon

13332

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:
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?