Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convention for generic and non-generic types with same name

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?

like image 633
Gigi Avatar asked Dec 26 '15 00:12

Gigi


People also ask

What is the generic and non-generic collections?

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.

Can a generic class be subclass of non-generic?

A generic class can extend a non-generic class.

How does a generic method differ from a generic type?

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.


2 Answers

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.

like image 76
Sergey Kalinichenko Avatar answered Sep 20 '22 05:09

Sergey Kalinichenko


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.

like image 23
Kyle Avatar answered Sep 20 '22 05:09

Kyle