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
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.
I use these to mock a DbSet when testing against classes that use EntityFramework Async methods.
And use them like so in my unit tests - saves a lot of time and can use them for any entity types:
You're right: they're not much use. Perhaps there are some rare cases that are exceptions, though. For example, what if the class was a type-specific repository, as in Justin's example, but kept a static collection as a cache? In general, if it contains state, not just methods, there may be a point to this.
A static generic class is exactly as useful as any given static class. The difference is that you don't have to use copy-and-paste to create a version of the static class for each type you want it to work on. You make the class generic, and you can "generate" one version for each set of type parameters.