What are the key uses of a Static Generic Class in C#? When should they be used? What examples best illustrate their usage?
public static class Example<T>
public static ...
I use static generic classes for caching reflection-heavy code.
Let's say I need to build an expression tree that instantiates objects. I build it once in the static constructor of the class, compile it to a lambda expression, then cache it in a member of the static class. I often don't make these classes publicly assessable - they are usually helpers for other classes. By caching my expressions in this way, I avoid the need to cache my expressions in some sort of
There is an example of this pattern in the BCL. The (
DataRow) extension methods
SetField<T>() use the (private) static generic class
System.Data.DataRowExtensions+UnboxT<T>. Check it out with Reflector.