Convention for Filenames of Generic Classes [closed]

Solution 1:

At Microsoft, they use ClassNameOfT.cs.

Solution 2:

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:

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