Gigi
Gigi

Reputation: 29491

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?

Upvotes: 7

Views: 5137

Answers (2)

Kyle
Kyle

Reputation: 6684

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.

Upvotes: 3

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726919

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.

Upvotes: 8

Related Questions