Convention for Filenames of Generic Classes

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.


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


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


At Microsoft, they use ClassNameOfT.cs.

Tags:

C#

Generics