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

Simple Singleton Pattern in C#

Rate me:
Please Sign up or sign in to vote.
4.85/5 (5 votes)
6 Nov 2011CPOL1 min read 19.6K   11
I'll be shot for posting this as an alternative, but I'm too curious for the answer.Shashank Bisen wrote: Ensure a class only has one instance.Provide a global point of access to it....As stated above, a singleton is a class that can be instantiated once, and only once.What is the...
I'll be shot for posting this as an alternative, but I'm too curious for the answer.

Shashank Bisen wrote:
Ensure a class only has one instance.
Provide a global point of access to it.
...
As stated above, a singleton is a class that can be instantiated once, and only once.

What is the benefit of being able to instantiate a singleton? AFAIK, it'd be used for lazy loading, otherwise one could simply create a static class and be done with it. Or even a class with a private constructor and some shared members. :)

From MSDN

As is the case with all class types, the type information for a static class is loaded by the .NET Framework common language runtime (CLR) when the program that references the class is loaded. The program cannot specify exactly when the class is loaded. However, it is guaranteed to be loaded and to have its fields initialized and its static constructor called before the class is referenced for the first time in your program. A static constructor is only called one time, and a static class remains in memory for the lifetime of the application domain in which your program resides.

There are also three examples on MSDN, including a "multithreaded" singleton:
Implementing a singleton in C#[^]

License

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


Written By
Software Developer Currently none.
Netherlands Netherlands
I'm a Delphi-convert, mostly into WinForms and C#. My first article is from 2001, extending the Delphi-debugger, which is still visible on the WayBackMachine[^] and even available in Russian[^] Smile | :)

Comments and Discussions

 
GeneralRe: In my opinion there's very little need for Singleton to begi... Pin
PIEBALDconsult25-Feb-12 3:04
mvePIEBALDconsult25-Feb-12 3:04 
GeneralRe: In my opinion there's very little need for Singleton to begi... Pin
Marco Bertschi6-Feb-14 10:47
protectorMarco Bertschi6-Feb-14 10:47 
GeneralRe: In my opinion there's very little need for Singleton to begi... Pin
PIEBALDconsult6-Feb-14 12:59
mvePIEBALDconsult6-Feb-14 12:59 
GeneralRe: In my opinion there's very little need for Singleton to begi... Pin
Marco Bertschi6-Feb-14 21:08
protectorMarco Bertschi6-Feb-14 21:08 
GeneralRe: In my opinion there's very little need for Singleton to begi... Pin
PIEBALDconsult7-Feb-14 6:53
mvePIEBALDconsult7-Feb-14 6:53 
GeneralRe: In my opinion there's very little need for Singleton to begi... Pin
Marco Bertschi7-Feb-14 10:41
protectorMarco Bertschi7-Feb-14 10:41 
GeneralRe: In my opinion there's very little need for Singleton to begi... Pin
PIEBALDconsult7-Feb-14 12:30
mvePIEBALDconsult7-Feb-14 12:30 
GeneralRe: In my opinion there's very little need for Singleton to begi... Pin
Marco Bertschi8-Feb-14 0:45
protectorMarco Bertschi8-Feb-14 0:45 
GeneralReason for my vote of 3 What does this add beyond a referenc... Pin
Henry.Ayoola7-Nov-11 23:25
Henry.Ayoola7-Nov-11 23:25 
GeneralRe: It's not another singleton-alternative, but an alternative a... Pin
Eddy Vluggen8-Nov-11 3:53
professionalEddy Vluggen8-Nov-11 3:53 
GeneralReason for my vote of 5 That link you provide is golden and ... Pin
Kabwla.Phone6-Nov-11 21:26
Kabwla.Phone6-Nov-11 21:26 

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.