Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Simple Singleton Pattern in C#

By , 6 Nov 2011
Rate this:
Please Sign up or sign in to vote.
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. Smile | :)
 
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)

About the Author

Eddy Vluggen
Software Developer
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

 
GeneralAll aspects of singleton pattern PinmemberPeace ON20-Jul-13 0:48 
GeneralRe: In my opinion there's very little need for Singleton to begi... PinmemberPIEBALDconsult25-Feb-12 3:04 
GeneralRe: In my opinion there's very little need for Singleton to begi... PinprofessionalMarco Bertschi6-Feb-14 10:47 
GeneralRe: In my opinion there's very little need for Singleton to begi... PinmemberPIEBALDconsult6-Feb-14 12:59 
GeneralRe: In my opinion there's very little need for Singleton to begi... PinprofessionalMarco Bertschi6-Feb-14 21:08 
GeneralRe: In my opinion there's very little need for Singleton to begi... PinmemberPIEBALDconsult7-Feb-14 6:53 
GeneralRe: In my opinion there's very little need for Singleton to begi... PinprofessionalMarco Bertschi7-Feb-14 10:41 
GeneralRe: In my opinion there's very little need for Singleton to begi... PinmemberPIEBALDconsult7-Feb-14 12:30 
GeneralRe: In my opinion there's very little need for Singleton to begi... PinprofessionalMarco Bertschi8-Feb-14 0:45 
GeneralReason for my vote of 3 What does this add beyond a referenc... PinmemberHenry.Ayoola7-Nov-11 23:25 
GeneralRe: It's not another singleton-alternative, but an alternative a... PinmemberEddy Vluggen8-Nov-11 3:53 
GeneralReason for my vote of 5 That link you provide is golden and ... PinmemberKabwla.Phone6-Nov-11 21:26 
Reason for my vote of 5
That link you provide is golden and should close this article.

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 | Mobile
Web02 | 2.8.140415.2 | Last Updated 6 Nov 2011
Article Copyright 2011 by Eddy Vluggen
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid