Click here to Skip to main content
11,584,556 members (70,456 online)
Click here to Skip to main content

Tagged as

Simple Singleton Pattern in C#

, 27 Oct 2011 CPOL 5.1K 2
Rate this:
Please Sign up or sign in to vote.
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?

License

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

Share

About the Author

ForgaSw
Peru Peru
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionTry this (Singleton with/without Lazy) Pin
BhavyaJ3-Mar-15 18:49
memberBhavyaJ3-Mar-15 18:49 
QuestionSingleton Pattern Example Pin
Developer Hemu Patel8-Aug-14 4:13
memberDeveloper Hemu Patel8-Aug-14 4:13 
GeneralMy vote of 4 Pin
Peace ON20-Jul-13 0:33
memberPeace ON20-Jul-13 0:33 
GeneralReason for my vote of 1 I'm a big fan of alternative 8, that... Pin
Kabwla.Phone6-Nov-11 21:27
memberKabwla.Phone6-Nov-11 21:27 
GeneralWhile Alternative 4 will only load the instance when it is c... Pin
Andrew Rissing27-Oct-11 5:23
memberAndrew Rissing27-Oct-11 5:23 
While Alternative 4 will only load the instance when it is called, it could have some concurrency issues as two threads could call it instantiating two separate instances. Alternative 3 would not have concurrency issues, but would instantiate the instance the first time the class type is referenced in code.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150707.1 | Last Updated 27 Oct 2011
Article Copyright 2011 by ForgaSw
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid