Convention for Filenames of Generic Classes

后端 未结 11 1089
广开言路
广开言路 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:47

    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.

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

    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

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

    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.

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

    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

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

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

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