Convention for Filenames of Generic Classes

后端 未结 11 1088
广开言路
广开言路 2020-12-08 00:20

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 version

相关标签:
11条回答
  • 2020-12-08 00:37

    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.

    0 讨论(0)
  • 2020-12-08 00:38

    At Microsoft, they use ClassNameOfT.cs.

    0 讨论(0)
  • 2020-12-08 00:39

    Sometimes I also see ClassName{T}.cs but it is common to name it ClassNameOfT.cs (like mentioned before Microsoft uses it)

    EntityFrameworkCore project(also Microsoft's) uses ClassName`.cs

    0 讨论(0)
  • 2020-12-08 00:39

    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.

    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.

    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>.

    0 讨论(0)
  • 2020-12-08 00:40

    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.

    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

    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...

    0 讨论(0)
  • 2020-12-08 00:47

    All new Microsoft classes use generics. The Queue and ArrayList were there before generics came out. Generics is the way forward.

    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.

    0 讨论(0)
提交回复
热议问题