Task
is an example of a class that exists in generic and non-generic form. The generic form extends the non-generic form.
public class Task
{
}
public class Task<T> : Task
{
}
Say I was implementing something of the sort myself. Normally, the convention is to put different classes in different files. Since they have the same name, it is not possible here.
What is the convention for this sort of scenario? Keep both classes in the same file, or put the generic class in a different file, but with a different name?
A Generic collection is a class that provides type safety without having to derive from a base collection type and implement type-specific members. A Non-generic collection is a specialized class for data storage and retrieval that provides support for stacks, queues, lists and hashtables.
A generic class can extend a non-generic class.
From the point of view of reflection, the difference between a generic type and an ordinary type is that a generic type has associated with it a set of type parameters (if it is a generic type definition) or type arguments (if it is a constructed type). A generic method differs from an ordinary method in the same way.
Although Microsoft does not publish a standard way of dealing with this situation, they put Task<TResult>
in a different file, with the name that is unrelated to Task
: non-generic class is in the file Task.cs
, while generic one is in the file Future.cs
.
Similarly, Expression
and Expression<TResult>
are located in different files (Expression.cs
and LambdaExpression.cs
).
It appears that the logic behind placement of classes in files is that the class goes with other classes with which it logically belongs.
The convention I've adopted (and subsequently encouraged at my workplace) is to add the generic parameters to the file name. So, for example, we'd have Task.cs
for the non-generic version and Task(T).cs
for the generic version. The reasoning behind this is largely due to us very strictly enforcing a one class per file rule.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With