如何来命名泛型类C#源文件 [英] How to name C# source files for generic classes

查看:566
本文介绍了如何来命名泛型类C#源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想坚持到通用命名约定如设计准则类库开发。我把每一个类型到它自己的源文件(和部分课程将在几个文件中的问题的命名为局部类文件约定),使用类型为文件名的名称。

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.

例如:

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 { }                 //  also in file Bag.cs ???
}

我应该把两种非通用和通用类的code到同一 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?

推荐答案

在<一个href=\"https://github.com/dotnet/corefx/tree/master/src/System.Collections.Immutable/src/System/Collections/Immutable\"相对=nofollow> CoreFX GitHub上库中,微软是继返回剔约定中的的马蒂亚斯Jakobsson着答案:

In the CoreFX GitHub repository, Microsoft is following the back-tick convention described in Matthias Jakobsson's answer:

所以基本上:

class ImmutableArray { }                      // ImmutableArray.cs
class ImmutableArray<T> { }                   // ImmutableArray`1.cs
...
class ImmutableDictionary<TKey, TValue> { }   // ImmutableDictionary`2.cs

和其他类中定义的类,名字是由外部类,然后按 + 组成,并由内部类的名称(外+ Inner.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 { }
}

这篇关于如何来命名泛型类C#源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆