简体   繁体   English

具有相同名称的通用和非泛型类型的约定

[英]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. Task是以泛型和非泛型形式存在的类的示例。 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? 将两个类保留在同一个文件中,或将泛型类放在不同的文件中,但名称不同?

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 . 虽然Microsoft没有发布处理这种情况的标准方法,但是他们将Task<TResult>放在一个不同的文件中,其名称与Task无关:非泛型类在Task.cs文件中,而泛型类是在Future.cs文件中。

Similarly, Expression and Expression<TResult> are located in different files ( Expression.cs and LambdaExpression.cs ). 类似地, ExpressionExpression<TResult>位于不同的文件中( Expression.csLambdaExpression.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. 因此,例如,我们有非通用版本的Task.cs和通用版本的Task(T).cs The reasoning behind this is largely due to us very strictly enforcing a one class per file rule. 这背后的原因主要是由于我们非常严格地执行每个文件规则一个类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM