Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Simple Singleton Pattern in C#

Rate me:
Please Sign up or sign in to vote.
3.83/5 (20 votes)
8 Jul 2011CPOL 33.3K   12   14
This should do it as well:class Singleton { public static readonly Singleton m_Instance = new Singleton(); // Prevent instance creation from other classes private Singleton() { } public static Singleton Instance { get { return m_Instance; } }}And it is "Singleton",...

This should do it as well:


C#
class Singleton {
    public static readonly Singleton m_Instance = new Singleton();

    // Prevent instance creation from other classes
    private Singleton() { }

    public static Singleton Instance { get { return m_Instance; } }
}

And it is "Singleton", not a "single Ton"... SCNR

License

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



Comments and Discussions

 
QuestionVote 2 Pin
Sinisa Hajnal23-Mar-16 1:21
professionalSinisa Hajnal23-Mar-16 1:21 
GeneralMy vote of 2 Pin
Bartlomiej Filipek6-Oct-13 23:31
Bartlomiej Filipek6-Oct-13 23:31 
GeneralMy vote of 2 Pin
Akhil Mittal26-Feb-13 22:54
professionalAkhil Mittal26-Feb-13 22:54 
GeneralMy vote of 2 Pin
Oleksandr Kulchytskyi29-Dec-12 3:19
professionalOleksandr Kulchytskyi29-Dec-12 3:19 
GeneralReason for my vote of 2 This alternative is not 'lazyloading... Pin
Kabwla.Phone1-Nov-11 3:02
Kabwla.Phone1-Nov-11 3:02 
GeneralRe: Reason for my vote of 2This alternative is not 'lazyloading... Pin
Ian A Davidson10-Apr-13 15:19
Ian A Davidson10-Apr-13 15:19 
GeneralAn exception thrown inside Singletin's .ctor will be thrown ... Pin
Kelqualyn31-Oct-11 20:44
Kelqualyn31-Oct-11 20:44 
GeneralRe: An exception thrown inside Singletin's .ctor will be thrown ... Pin
Ian A Davidson10-Apr-13 15:33
Ian A Davidson10-Apr-13 15:33 
GeneralRe: An exception thrown inside Singletin's .ctor will be thrown ... Pin
Kelqualyn11-Apr-13 1:27
Kelqualyn11-Apr-13 1:27 
GeneralRe: An exception thrown inside Singletin's .ctor will be thrown ... Pin
Ian A Davidson11-Apr-13 2:04
Ian A Davidson11-Apr-13 2:04 
My answer was in response to your assertion:
Quote:
An exception thrown inside Singletin's .ctor will be thrown on type initialization (instead of first access to Instance property and even if you will never use it)
This is not quite correct! If you never call "Instance", the exception will never be thrown, even if you explicitly create one like this (because the type will not ever get initialised). In other words, the exception would still get thrown the first time the class is required, i.e. in the former case, "just before" the call to Instance is made.

You are correct that if there should be an error in the constructor then the former pattern can be harder to debug. However, I wonder if you've forgotten that an exception should be just that - exceptional! It may well be that if the instance class fails to be instantiated that the application could not continue anyway, so allowing the application to terminate can be the best option. And if you have a very simple class that should not throw an exception, then the former pattern may be better (e.g. it gets around the need for your own locking mechanism in the Instance method, and the overhead involved in that. Although of course, you mustn't forget that you may need to allow for locking mechanisms in the class's members!)

This is why I will not say that one pattern is better than the other. It's all about design.

As I said in my reply to Kabwla.Phone[^], there are pros and cons to each pattern. I nearly mentioned the problem of the difficulty to debug, but must have decided not to do so as my responses were all getting rather long.

Regards,
Ian.
GeneralReason for my vote of 5 the simplest and because of it the b... Pin
Robert Krzysztof Winkler31-Oct-11 2:03
Robert Krzysztof Winkler31-Oct-11 2:03 
GeneralRe: Reason for my vote of 5the simplest and because of it the b... Pin
Ian A Davidson10-Apr-13 14:37
Ian A Davidson10-Apr-13 14:37 
GeneralThe field should probably be private. Pin
Richard Deeming13-Jul-11 8:11
mveRichard Deeming13-Jul-11 8:11 

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

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