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

Tagged as

Simple Singleton Pattern in C#

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

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):

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)

Share

About the Author

Kabwla.Phone
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 appliction or a patient.
Oddly enough, more people care about the death of their application, than the massacre op people...

Comments and Discussions

 
GeneralReason for my vote of 1It doesn't prevent the creation of o... [modified] PinmemberPIEBALDconsult25-Feb-12 3:11 
GeneralReason for my vote of 1 Almost a copy-paste from: http://msd... PinmemberForgaSw7-Nov-11 2:59 
GeneralRe: There is a difference between 7 and 8. Alternative 8 impleme... PinmemberKabwla.Phone7-Nov-11 3:09 
GeneralReason for my vote of 1 Alternative 8, put the reference. In... PinmemberForgaSw7-Nov-11 2:58 
GeneralRe: I once wrote a very stupid "delphi joke" as a comment in my ... PinmemberKabwla.Phone7-Nov-11 3:14 
GeneralA former Delphi-Programmer? :) PinmemberEddy Vluggen1-Nov-11 10:13 
GeneralRe: Actually, yes. Not that it has anything to do with the solut... PinmemberKabwla.Phone1-Nov-11 22:03 

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
Web04 | 2.8.140827.1 | Last Updated 6 Nov 2011
Article Copyright 2011 by Kabwla.Phone
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid