Click here to Skip to main content
15,879,095 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 
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 
I once wrote a very stupid "delphi joke" as a comment in my code. It went:

The return value is passed by reference,
as a result, this procedure does not function.

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.