In Pro C# by Andrew Tolson, the writer says that when a non generic class extends a generic base class, the derived class must specify the type parameter.
// Assume you have created a custom
// generic list class.
public class MyList<T>
{
private List<T> listOfData = new List<T>();
}
// Non-generic classes must specify the type
// parameter when deriving from a
// generic base class.
public class MyStringList : MyList<string>
{}
What I don’t understand is why this is necessary?
Well, non-generic classes do not have type parameters, and generic classes have one or more type parameter.
If you inherit the class from a generic class, without specifying the type parameter, you still have a generic class, i.e
but
so
or, more general
A more wordy explanation, mostly because I love using the word “arity“.
Classes in the CLR can have arity of zero or more, meaning that they can specify zero or more type parameters. However, the CLR cannot instantiate classes with non-zero arity, so in order to do anything useful, the arity of the class must be brought down to zero.
That means, that while we can partially specify classes like:
that decrease the arity, or even declare classes like
that increase the arity, the story has to end with a class with an arity of 0, like
List<string>,Dictionary<int, double>etc…