C# 通用类文件名约定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/804036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 01:51:17  来源:igfitidea点击:

Convention for Filenames of Generic Classes

c#generics

提问by Waylon Flinn

I want to be able to distinguish between a generic and regular (non-generic) version of a class. Much like the .NET framework does with it's generic and non-generic versions of several of it's interfaces and collection classes. (Queue, Queue(T))

我希望能够区分类的泛型和常规(非泛型)版本。就像 .NET 框架对它的几个接口和集合类的通用和非通用版本所做的一样。(队列,队列(T))

I generally like to follow the convention of one class per file (as in Java). Is there a common convention for naming files containing a single generic class? I'm mostly interested in Windows (NTFS specifically) but it seems like a good convention would be (at least a little) portable.

我通常喜欢遵循每个文件一个类的约定(如在 Java 中)。是否有命名包含单个泛型类的文件的通用约定?我最感兴趣的是 Windows(特别是 NTFS),但似乎一个好的约定(至少有一点)是可移植的。

采纳答案by Mark Cidade

At Microsoft, they use ClassNameOfT.cs.

在 Microsoft,他们使用ClassNameOfT.cs.

回答by Andrew Hare

How about:

怎么样:

Type.cs

and

TypeGeneric.cs

Whenever I have done this in the past I have always put both types in one file with the non-generic type as the file name. I think that this makes things pretty clear as .NET has no conventions/restrictions on one type per file like Java does.

过去每当我这样做时,我总是将两种类型放在一个文件中,并将非通用类型作为文件名。我认为这让事情变得非常清楚,因为 .NET 没有像 Java 那样对每个文件的一种类型进行约定/限制。

But if you must then I would suggest something like I have above, and using a suffix will make the files show up together in any alphabetized list (Solution Explorer, Windows Explorer, etc.).

但是,如果您必须这样做,我会建议类似上面的内容,并且使用后缀将使文件一起显示在任何按字母顺序排列的列表中(解决方案资源管理器、Windows 资源管理器等)。

Here is another idea:

这是另一个想法:

Type`1.cs

This would allow you to break out different generic types by the number of generic type parameters they accepted. Its just a thought though as I still think it would be simpler to just put all the types in one file.

这将允许您通过它们接受的泛型类型参数的数量来划分不同的泛型类型。这只是一个想法,因为我仍然认为将所有类型放在一个文件中会更简单。

回答by BFree

I'd probably have two folders in the project, something like Gereric, NonGeneric or something like that. They can still be in the same namespace, and then they can both have the same file name. Just a thought...

我可能在项目中有两个文件夹,比如 Gereric、NonGeneric 或类似的东西。它们仍然可以在同一个命名空间中,然后它们都可以具有相同的文件名。只是一个想法...

回答by tofi9

All new Microsoft classes use generics. The Queueand ArrayListwere there before generics came out. Generics is the way forward.

所有新的 Microsoft 类都使用泛型。将QueueArrayList在那里的仿制药出来之前。泛型是前进的方向。

The convention for one-class-per-single file is to name the filename after the class name (whether generic of not). For MyClass, you'll have MyClas.cs. For every new namespace you'll need to create a new folder. This is how Visual Studio also works.

每个单个文件一个类的约定是在类名之后命名文件名(无论是否通用)。对于 MyClass,您将拥有 MyClas.cs。对于每个新命名空间,您都需要创建一个新文件夹。这也是 Visual Studio 的工作方式。

回答by Fredrik M?rk

I would probably put them in folders and use the namespace mechanism instead. You can compare with System.Collections vs. System.Collections.Generic. On the other hand, if it's more common than not that the classes use generics, perhaps it's better to point out those that are not. That is if you really want to separate the generic classes from other classes. Personally I usually don't bother to do that, since I don't really see a practical benefit from it.

我可能会将它们放在文件夹中并改用命名空间机制。您可以与 System.Collections 与 System.Collections.Generic 进行比较。另一方面,如果类使用泛型更为常见,也许最好指出那些不是。也就是说,如果您真的想将泛型类与其他类分开。就我个人而言,我通常不会费心这样做,因为我并没有真正看到它的实际好处。

回答by Joe

From the responses so far it seems there isn't a consensus.

从目前的反应来看,似乎没有达成共识。

Using the same filename in a sub-namespace (and sub-folder) "Generics" (like System.Collecctions.Generics) is an option. But it's not always desirable to create a new namespace.

在子命名空间(和子文件夹)“Generics”(如 System.Collections.Generics)中使用相同的文件名是一种选择。但是创建一个新的命名空间并不总是可取的。

For example, in an existing namespace with non-generic classes that are maintained for backwards compatibility, but marked with ObsoleteAttribute, it's probably better to keep the generic versions in the same namespace.

例如,在具有为向后兼容而维护的非泛型类的现有命名空间中,但用 ObsoleteAttribute 标记,最好将泛型版本保留在同一命名空间中。

I think a suffix is a reasonable way to go. I've adopted a convention of using the type parameters as a suffix (so: MyClassT for MyClass<T>, or MyDictionaryKV for MyDictionary<K,V>.

我认为后缀是一种合理的方法。我采用了使用类型参数作为后缀的约定(因此:MyClassT 代表 MyClass<T>,或 MyDictionaryKV 代表 MyDictionary<K,V>。

回答by Mark Cidade

Just found this question after looking for what conventions other people use for generic class filenames.

在寻找其他人用于通用类文件名的约定之后才发现这个问题。

Lately I've been using ClassName[T].cs. I really like this convention, and I think it's superior to the others for the following reasons:

最近一直在用ClassName[T].cs。我真的很喜欢这个约定,我认为它优于其他约定,原因如下:

  • The type parameters jump out at you a little more than they do with the Microsoft convention (e.g., ClassNameOfT.cs).
  • It allows you to have multiple type parameters without too much confusion: Dictionary[TKey, TValue].cs
  • It doesn't require you to create any special folders, or to have your generic classes in a special namespace. If you only have a few generic classes, having a special namespace dedicated to them just isn't practical.
  • 与 Microsoft 约定(例如,ClassNameOfT.cs)相比,类型参数对您的影响更大一些 。
  • 它允许您拥有多个类型参数而不会造成太多混淆: Dictionary[TKey, TValue].cs
  • 它不需要您创建任何特殊的文件夹,或将您的通用类放在特殊的命名空间中。如果您只有几个泛型类,那么为它们专门设置一个特殊的命名空间是不切实际的。

I borrowed this convention from Boo's generic syntax, albeit slightly modified (Boo uses ClassName[of T]).

我从Boo的通用语法中借用了这个约定,尽管稍作修改(Boo 使用ClassName[of T])。

Some developers seem to have a phobia of filenames that contain anything but letters and underscores, but once you can get past that this convention seems to work extremely well.

一些开发人员似乎对包含字母和下划线以外的任何文件名有恐惧症,但是一旦你克服了这个约定似乎非常有效。

回答by anon

Don't use the grave accent ` in your generic file names if you're running Visual Studio 2008. There's a known issue with them that causes breakpoints to fail:

如果您运行的是 Visual Studio 2008,请不要在通用文件名中使用重音符 `。它们有一个已知问题会导致断点失败:

http://connect.microsoft.com/VisualStudio/feedback/details/343042/grave-accent-in-filename-causes-failure-to-recognize-target-language-breakpoints-fail

http://connect.microsoft.com/VisualStudio/feedback/details/343042/grave-accent-in-filename-causes-failure-to-recognize-target-language-breakpoints-fail

回答by Wim.van.Gool

I see that this topic has been abandoned more than a year ago, but still I would like to share my view on this convention.

我看到这个话题在一年多前就被放弃了,但我仍然想分享我对这个公约的看法。

First of all, having multiple classes that have the same name but only differ in the amount of type-parameters isn't always a case of backwards compatibility. Surely, you don't see it very often, but the new Action- and Func-classes of .NET were just designed this way, and I'm currently implementing something similar.

首先,拥有多个具有相同名称但仅类型参数数量不同的类并不总是向后兼容的情况。当然,您不会经常看到它,但是 .NET 的新 Action- 和 Func- 类就是这样设计的,我目前正在实现类似的东西。

For clarity and distinguishability, I use the following convention that only specifies the number of generic arguments for a given type:

为了清晰和可区分,我使用以下约定仅指定给定类型的泛型参数数量:

  • MyClass.cs
  • MyClass.T1.cs
  • MyClass.T2.cs
  • MyClass.cs
  • MyClass.T1.cs
  • MyClass.T2.cs

This way, my filenames stay short and simple while still clearly communicating the class-name and the different amount of type parameters at the cost of a simple extra dot (which is, in my experience, a commonly accepted thing to do in a filename and looks much better than comma's and other non-alpanumeric characters, but this is just a matter of taste I guess). Putting the names (or acronyms) of the type parameters just lengthens the filenames while at this level I'm not really interested in the actual names of the type parameters anyway...

这样,我的文件名保持简短,同时仍然清楚地传达类名和不同数量的类型参数,代价是一个简单的额外点(根据我的经验,这是一个普遍接受的文件名和看起来比逗号和其他非字母数字字符好得多,但这只是我猜的品味问题)。放置类型参数的名称(或首字母缩写词)只会延长文件名,而在这个级别上,无论如何我对类型参数的实际名称并不真正感兴趣......

回答by Fred

Personally I wouldn't use the grave accent notation:

我个人不会使用重音符号:

Foo.cs
Foo`1.cs

For the simple reason that I am scared of the grave accent. Not only does it have a scary name , but I am unsure how it will be handled by different file systems, version control systems and in URLs. Hence, I would prefer to stick to common alphanumeric characters.

原因很简单,我害怕重口音。它不仅有一个可怕的名字,而且我不确定不同的文件系统、版本控制系统和 URL 将如何处理它。因此,我更愿意坚持使用常见的字母数字字符。

NameOfT.csseems to be used in ASP.NET Core according to a search on GitHub. 19 results. Reference.

NameOfT.cs根据在 GitHub 上的搜索,似乎在 ASP.NET Core 中使用。19 个结果。参考

Also limited use in CoreFX. 3 results. Reference.

在 CoreFX 中的使用也受到限制。3 个结果。参考

Example:

例子:

Foo.cs
FooOfT.cs