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.
public static class Example<T>
{
public static ...
}
Since you can't define extension methods in them they appear to be somewhat limited in their utility. Web references on the topic are scarce so clearly there aren't a lot of people using them. Here's a couple:-
http://ayende.com/Blog/archive/2005/10/05/StaticGenericClass.aspx
http://stackoverflow.com/questions/686630/static-generic-class-as-dictionary
Summary of Answers Given
The key issues appear to be "What's the difference between a static generic class with static methods and a non-generic static class with static generic members?"
The decision as to which to use appears to revolve around "Does the class need to store type-specific state internally?"
If there is no need for type-specific internal storage then a static non-generic class with generic static methods appears to be preferable because the calling syntax is nicer and you can define extension methods within it.
@Steven: said "Static fields of a generic type are specific to the actual type T. This means you can store a type specific cache internally".
@Justin Niessner: "To have shared private static resources that match the type of T if you needed"
@Steven Sudit: "In general, if it contains state, not just methods, there may be a point to this."
Others pointed out the benefits or downsides of each relating to how they were used:-
@Chris Taylor: Define a constraint on the class level for the type, then the constraint applies to all the static members of the class.
@Juliet: static classes with generic methods when "your static class holds no mutable state" make your "client code ... easier to read".
Some gave specific advice on which to use and scenarios:
@Juliet: "I think in general, you should avoid creating type parameters on static classes."
@Greg: "I use static generic classes for caching reflection-heavy code." and "I avoid the need to cache my expressions in some sort of Dictionary". He also pointed out: "these classes aren't publicly assessable, they are usually helpers for other classes" which addresses the issue of clumsy syntax pointed out by others.
In the end I marked Greg's answer as accepted because it contained the best example and reasoning, but there were lots of others who added value here.