What are the key uses of a Static Generic Class in C#? When should they be used? What examples best illustrate their usage?
e.g.
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 Dictionary
.
There is an example of this pattern in the BCL. The (DataRow
) extension methods Field
and SetField
use the (private) static generic class System.Data.DataRowExtensions+UnboxT
. Check it out with Reflector.