Singleton implementation using generics I did this as an exercise just to practice/improve using generics.

Independent of how useful this implementation of a Singleton is, how is my coding in terms of using generics and any other aspect of class design of code style?

void Main()
{
    var a = Singleton&lt;MyClass&gt;.Value;
    var b = Singleton&lt;MyClass, MyClassFactory&gt;.Value;
    var c = Singleton&lt;MyClass&gt;.Value;
    var d = Singleton&lt;MyClass, MyClassFactory&gt;.Value;
    var e = Singleton&lt;MyOtherClass&gt;.Value;
    var f = Singleton&lt;MyOtherClass&gt;.Value;
    var g = Singleton&lt;MyOtherClass, MyOtherFactory&gt;.Value;
    var h = Singleton&lt;MyOtherClass, MyOtherFactory&gt;.Value;
}

class SingletonBase
{
    protected static object Locker = new LockerObject();
}

class Singleton&lt;T&gt; : SingletonBase where T : new() 
{
    static T StaticT;

    public static T Value 
    {
        get
        {
            lock (Locker)
            {
                if(StaticT == null)
                {
                    StaticT = Activator.CreateInstance&lt;Factory&lt;T&gt;&gt;().Create();
                }
                else
                {
                    Console.WriteLine ("Singleton&lt;T&gt;::Value" + typeof(T).Name + " is already created");
                }
            }
            return StaticT;
        }
    }
}

class Singleton&lt;T, F&gt; : SingletonBase where T : new() where F : IFactory&lt;T&gt;, new()
{
    static T StaticT;

    public static T Value 
    {
        get
        {
            lock (Locker)
            {
                if(StaticT == null)
                {
                    StaticT = new F().Create();
                }
                else
                {
                    Console.WriteLine ("Singleton&lt;T, F&gt;::Value" + typeof(T).Name + " is already created");
                }
            }
            return StaticT;
        }
    }
}

class LockerObject
{
    Guid myGUID;
    public LockerObject()
    {
        this.myGUID = Guid.NewGuid();
        Console.WriteLine ("New LockerObject " + this.myGUID.ToString());
    }
}

interface IFactory&lt;T&gt;
{
    T Create();
}

class Factory&lt;T&gt; : IFactory&lt;T&gt; where T : new()
{
    public T Create()
    {
        Console.WriteLine ("Factory&lt;T&gt;::Create()");  
        return new T();
    }
}

class MyClassFactory : IFactory&lt;MyClass&gt;
{
    public MyClass Create()
    {
        Console.WriteLine ("MyClassFactory::Create()");    
        return new MyClass();
    }
}

class MyClass
{
    public MyClass()
    {
        Console.WriteLine ("MyClass created");
    }
}


class MyOtherClass
{
    public MyOtherClass()
    {
        Console.WriteLine ("MyOtherClass created");
    }
}

class MyOtherFactory : IFactory&lt;MyOtherClass&gt;
{
    public MyOtherClass Create()
    {
        Console.WriteLine ("MyOtherFactory::Create()");    
        return new MyOtherClass();
    }
}


Output:




New LockerObject 36aa2282-d745-43ca-84d2-998a78e39d51
Factory&lt;T&gt;::Create()
MyClass created
MyClassFactory::Create()
MyClass created
Singleton&lt;T&gt;::ValueMyClass is already created
Singleton&lt;T, F&gt;::ValueMyClass is already created
About your locking strategy to create the singleton, I would use a double check to avoid too much locking:

if(x)
{
    lock(Locker)
        if(x) //again once we got the lock
            DoStuff();
} 
else 
{
    FooBar();
}


Like this, you will hopefully use the lock only once when you first call the instance.