How to name C# source files for generic classes
I am trying to stick to general naming conventions such as those described in Design Guidelines for Developing Class Libraries. I put every type into its own source file (and partial classes will be split across several files as described in question Naming Conventions for Partial Class Files), using the name of the type as the file name.
Examples:
namespace Demo.Bla // project
{
enum FlowDirection { } // in file FlowDirection.cs
class LayoutManager { } // in file LayoutManager.cs
}
namespace Demo.Bla.LayoutControllers // folder LayoutControllers in project
{
class StackPanelLayoutController { } // in file LayoutControllers/StackPanelLayoutController
}
But I am not sure I've come up with a clever way of naming source files which contain generic classes. Say that I have following classes, for instance:
namespace Demo.Bla.Collections // folder Collections
{
class Map<T> { } // in file Map.cs (obviously)
class Bag { } // in file Bag.cs (obviously)
class Bag<T> : Bag { } /开发者_如何学C/ also in file Bag.cs ???
}
Should I put the code of both the non-generic and the generic Bag
classes into the same Bag.cs
file? What are your habits?
I think the common solution to this problem is to name the file like this:
{ClassName}`{NumberOfGenericParameters}
This would give you this filename:
Bag.cs and Bag`1.cs
This is the way Microsoft handle this issue in frameworks like Asp.net Mvc.
I have seen some libraries using
Bag.cs
Bag`1.cs
Bag`2.cs
as this is what the Type.Name
would display.
I want to be more descriptive with the type parameters so I lately tend to use
Bag.cs
Bag{T}.cs
Bag{TKey, TValue}.cs
This is a format that is also supported by the XML comments.
/// <summary>
/// ...
/// Uses the <see cref="T:MyLibrary.Bag{TKey, TValue}" /> class.
/// </summary>
Usually if I have several types "overloaded" by the number of type parameters, it's either because one derives from the other or one is used to create the other. I just put them in the same file.
One alternative option would be to split them into different files, make one file the "primary" one, and the others "depend" on it in the build file, as per the partial class question you linked to in the question.
That way you could end up with a visual link in Visual Studio, but still one class per file to make it easier to work with them.
In the CoreFX GitHub repository, Microsoft is following the back-tick convention described in Matthias Jakobsson's answer:
So basically:
class ImmutableArray { } // ImmutableArray.cs
class ImmutableArray<T> { } // ImmutableArray`1.cs
...
class ImmutableDictionary<TKey, TValue> { } // ImmutableDictionary`2.cs
And for classes defined inside other classes, the name is composed by the outer class followed by +
and by the name of the inner class (Outer+Inner.cs
):
partial class ImmutableArray<T> // ImmutableArray`1+Builder.cs
{
class Builder { }
}
I add a suffix 'T' to the names of my generic classes.
class Bag { } // in file Bag.cs
class BagT<T> : Bag { } // in file BagT.cs
class BagInputs : BagT<Input> // in file BagInputs.cs
You asked,
Should I put the code of both the non-generic and the generic Bag classes into the same Bag.cs file? What are your habits?
The above convention of mine is non-standard; I should clarify that I was answering "what are my habits" and not necessarily "what you should do".
精彩评论