Convention for Filenames of Generic Classes

C#Generics

C# Problem Overview


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

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.

C# Solutions


Solution 1 - C#

At Microsoft, they use ClassNameOfT.cs.

Solution 2 - C#

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:

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

I borrowed this convention from Boo's generic syntax, albeit slightly modified (Boo uses 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.

Solution 3 - C#

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

Solution 4 - C#

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:

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

Solution 5 - C#

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.

NameOfT.cs seems to be used in ASP.NET Core according to a search on GitHub. 40 results. Reference.

Also used in the .NET Core runtime. 36 results. Reference.

Example:

Foo.cs
FooOfT.cs

Solution 6 - C#

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

Solution 7 - C#

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.

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

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.

Solution 8 - C#

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.

Solution 9 - C#

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.

Solution 10 - C#

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

Solution 11 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWaylon FlinnView Question on Stackoverflow
Solution 1 - C#Mark CidadeView Answer on Stackoverflow
Solution 2 - C#user22467View Answer on Stackoverflow
Solution 3 - C#Wim.van.GoolView Answer on Stackoverflow
Solution 4 - C#anonView Answer on Stackoverflow
Solution 5 - C#FredView Answer on Stackoverflow
Solution 6 - C#KonradView Answer on Stackoverflow
Solution 7 - C#Andrew HareView Answer on Stackoverflow
Solution 8 - C#tofi9View Answer on Stackoverflow
Solution 9 - C#Fredrik MörkView Answer on Stackoverflow
Solution 10 - C#JoeView Answer on Stackoverflow
Solution 11 - C#BFreeView Answer on Stackoverflow