Write a lock free singleton factory using generics in c#
A delegate can be seen as method signature contract. You can define what sort of methods are you willing to work with. For example


The method should accept type x, type y and type z as parameters
and should return with type w


Any method, which satisfy this contract, is a valid method from the delegate perspective.  



public delegate TType CreateObjectDelegate();


This delegate says that any method that has no parameter and will return with TType is a good one.

You can describe the same by using the Func class:

Func&lt;TType&gt; CreateObjectDelegate


Whenever you want to call the Register method you just have to pass a method which satisfies this contract. For example (let's suppose TType is int):

cacheProvider.Register("MyChild1Cache", () =&gt; 0);

Func&lt;int&gt; alternative1 = () =&gt; 1;
cacheProvider.Register("MyChild2Cache", alternative1.Invoke);

Func&lt;int, Func&lt;int&gt;&gt; alternative2 = i =&gt; () =&gt; i;
cacheProvider.Register("MyChild3Cache", alternative2.Invoke(2).Invoke);