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

Simple Singleton Pattern in C#

Rate me:
Please Sign up or sign in to vote.
1.91/5 (5 votes)
6 Nov 2011CPOL 10.7K   7
Make it a generic class and fix your problem forever. This works because the compiler turns the generic into a new class (something like SigletonManagerOfTypeParamterTypeName). So the static variables are not shared amongst instances...public static class Singleton where...

Make it a generic class and fix your problem forever. This works because the compiler turns the generic into a new class (something like SigletonManagerOfTypeParamterTypeName). So the static variables are not shared amongst instances...


C#
public static class Singleton<TSingletonType>
    where TSingletonType: class, new()
{
    private static volatile TSingletonType instance;
    private static object syncRoot = new Object();

    public static TSingletonType Instance
    {
        get
        {
            if (instance == null)
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new TSingletonType();
                }
            }

            return instance;
        }
    }
}

Usage (check it out, it works):


C#
Form frm = Singleton<Form>.Instance;
Control ctrl = Singleton<control>.Instance;

License

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


Written By
Software Developer (Senior)
Netherlands Netherlands
Doing that 'computer thing' ever since the C64.

Sometimes I feel that being a programmer is much like being a doctor: You just have to know everything and if you don't, something dies.

Either being an application or a patient.

Oddly enough, more people care about the death of their application, than the massacre of people...

Comments and Discussions

 
GeneralReason for my vote of 1It doesn't prevent the creation of o... Pin
PIEBALDconsult25-Feb-12 3:11
mvePIEBALDconsult25-Feb-12 3:11 
GeneralReason for my vote of 1 Almost a copy-paste from: http://msd... Pin
ForgaSw7-Nov-11 2:59
ForgaSw7-Nov-11 2:59 
GeneralRe: There is a difference between 7 and 8. Alternative 8 impleme... Pin
Kabwla.Phone7-Nov-11 3:09
Kabwla.Phone7-Nov-11 3:09 
There is a difference between 7 and 8.
Alternative 8 implements the 'Singleton pattern' for a SINGLE class (of type singleton) as aproved by Microsoft. My version implements the pattern for any class (of type TSingletonType) by the use of the generic class (Singleton) so you can put it in your core library, and will never have to implement it again.
Yes the structure of the code copied 1:1 from one of the implementations above, but I think I have added an important feature that very likely not everybody knows will work.

GeneralReason for my vote of 1 Alternative 8, put the reference. In... Pin
ForgaSw7-Nov-11 2:58
ForgaSw7-Nov-11 2:58 
GeneralRe: I once wrote a very stupid "delphi joke" as a comment in my ... Pin
Kabwla.Phone7-Nov-11 3:14
Kabwla.Phone7-Nov-11 3:14 
GeneralA former Delphi-Programmer? :) Pin
Eddy Vluggen1-Nov-11 10:13
professionalEddy Vluggen1-Nov-11 10:13 
GeneralRe: Actually, yes. Not that it has anything to do with the solut... Pin
Kabwla.Phone1-Nov-11 22:03
Kabwla.Phone1-Nov-11 22:03 

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.