布局 C# 类的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/603758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the best way to layout a C# class?
提问by Ray
Is there a standard way of laying out a C# file? As in, Fields, then Properties, then Constructors, etc?
是否有布置 C# 文件的标准方法?比如,字段,然后是属性,然后是构造函数等等?
Here's what I normally do, but I'm wondering if there's a standard way?
这是我通常做的,但我想知道是否有标准的方法?
- Nested Classes or Enums
- Fields
- Properties
- Events
- Constructors
- Public Methods
- Private Methods
- 嵌套类或枚举
- 字段
- 特性
- 活动
- 构造函数
- 公共方法
- 私有方法
Do people group their fields together, or do they put them with the properties? Or do people not worry about an order? Visual Studio seems to make it so hard to do.
人们是将他们的字段组合在一起,还是将它们与属性放在一起?或者人们不担心订单?Visual Studio 似乎很难做到。
Edit: Moved other part about ReSharper here: Make Resharper respect your preference for code order.
编辑:将有关 ReSharper 的其他部分移至此处:使 Resharper 尊重您对代码顺序的偏好。
采纳答案by chills42
I tend to use Microsoft StyleCop, which has a set order according to rule SA1201:
我倾向于使用Microsoft StyleCop,它根据规则SA1201有一个固定的顺序:
CauseAn element within a C# code file is out of order in relation to the other elements in the code.
Rule DescriptionA violation of this rule occurs when the code elements within a file do not follow a standard ordering scheme.
To comply with this rule, elements at the file root level or within a namespace must be positioned in the following order:
- Extern Alias Directives
- Using Directives
- Namespaces
- Delegates
- Enums
- Interfaces
- Structs
- Classes
Within a class, struct, or interface, elements must be positioned in the following order:
- Fields
- Constructors
- Finalizers (Destructors)
- Delegates
- Events
- Enums
- Interfaces
- Properties
- Indexers
- Methods
- Structs
- Classes
Complying with a standard ordering scheme based on element type can increase the readability and maintainability of the file and encourage code reuse.
When implementing an interface, it is sometimes desirable to group all members of the interface next to one another. This will sometimes require violating this rule, if the interface contains elements of different types. This problem can be solved through the use of partial classes.
Add the partial attribute to the class, if the class is not already partial.
Add a second partial class with the same name. It is possible to place this in the same file, just below the original class, or within a second file.
Move the interface inheritance and all members of the interface implementation to the second part of the class.
原因C# 代码文件中的某个元素相对于代码中的其他元素而言是乱序的。
规则描述当文件中的代码元素不遵循标准排序方案时,就会违反此规则。
为了遵守此规则,文件根级别或命名空间中的元素必须按以下顺序定位:
- 外部别名指令
- 使用指令
- 命名空间
- 代表
- 枚举
- 接口
- 结构
- 班级
在类、结构或接口中,元素必须按以下顺序放置:
- 字段
- 构造函数
- 终结器(析构函数)
- 代表
- 活动
- 枚举
- 接口
- 特性
- 索引器
- 方法
- 结构
- 班级
遵守基于元素类型的标准排序方案可以提高文件的可读性和可维护性,并鼓励代码重用。
在实现接口时,有时需要将接口的所有成员彼此相邻分组。如果界面包含不同类型的元素,这有时需要违反此规则。这个问题可以通过使用部分类来解决。
如果类还不是部分的,则将部分属性添加到类中。
添加具有相同名称的第二个分部类。可以将它放在同一个文件中,就在原始类的下面,或者放在第二个文件中。
将接口继承和接口实现的所有成员移动到类的第二部分。
回答by Michael Meadows
I think there's no bestway. There are two important things to consider when it comes to layout. The first most important thing is consistency. Pick an approach and make sure that the entire team agrees and applies the layout. Secondly, if your class gets big enough that you are searching for where those pesky properties live (or have to implement regions to make them easier to find), then your class is probably too large. Consider sniffing it, and refactoring based on what you smell.
我认为没有最好的方法。在布局方面,有两件重要的事情需要考虑。首先最重要的是一致性。选择一种方法并确保整个团队都同意并应用布局。其次,如果您的类变得足够大,以至于您正在搜索那些讨厌的属性所在的位置(或者必须实现区域以使其更容易找到),那么您的类可能太大了。考虑嗅探它,并根据你闻到的气味进行重构。
To answer the reshaper question, check under Type Members Layoutin Options(under the C#node). It's not simple, but it is possible to change the layout order.
要回答这个问题,整形,下检查类型成员布局的选项(下C#节点)。这并不简单,但可以更改布局顺序。
回答by plinth
I tend to clump private data and tend to clump related methods/properties in functional groups.
我倾向于将私有数据聚集在一起,并倾向于将功能组中的相关方法/属性聚集在一起。
public class Whatever {
// private data here
int _someVal = kSomeConstant;
// constructor(s)
public Whatever() { }
#region FabulousTrick // sometimes regionize it
// fabulous trick code
private int SupportMethodOne() { }
private double SupportMethodTwo() { }
public void PerformFabulousTrick(Dog spot) {
int herrings = SupportMethodOne();
double pieces = SupportMethodTwo();
// etc
}
#endregion FabulousTrick
// etc
}
回答by Chad Moran
Whatever makes your more productive. Some like private fields next to property accessors, some like fields together above the constructors. The biggest thing that can help is grouping "like," elements. I personally like bringing together private methods, private properties, etc.
无论是什么让你更有效率。有些喜欢在属性访问器旁边的私有字段,有些喜欢在构造函数上方的字段。最大的帮助是对“like”元素进行分组。我个人喜欢将私有方法、私有属性等组合在一起。
Try some things out and again, whatever you feel makes you more productive and helps you keep your code maintained.
反复尝试一些事情,无论你觉得什么都能提高你的工作效率并帮助你保持代码的维护。
回答by Pat
I don't believe regions are necessarily a sign of bad code. But to determine that you will have to review what you have. As I've stated herethis is how I regionize my code.
我不相信区域一定是错误代码的标志。但是要确定您必须查看您拥有的内容。正如我在这里所说的,这就是我对代码进行区域化的方式。
Enumerations
Declarations
Constructors
Methods
Event Handlers
Properties
枚举
声明
构造函数
方法
事件处理程序
属性
But the main thing is keeping it consistent and purposeful.
但最重要的是保持它的一致性和目的性。
回答by si618
Each to their own, but I tend to follow the same order that the MSDN help follows.
每个人都有自己的,但我倾向于遵循 MSDN 帮助遵循的相同顺序。
I also don't like to nest classes or enums, instead create separate files for them, that also makes writing unit tests easier (since it's easy to find the associated test file when you need to add/fix/refactor a test).
我也不喜欢嵌套类或枚举,而是为它们创建单独的文件,这也使编写单元测试更容易(因为当您需要添加/修复/重构测试时很容易找到相关的测试文件)。
IMHO the order isn't that important because VS makes it very easy to find all members (especially if you follow the one class/interface/enum per file approach), and Sandcastle will group them if you want to build docs, so I'd be more concerned about giving them meaningful names.
恕我直言,顺序并不是那么重要,因为 VS 可以很容易地找到所有成员(特别是如果您遵循一个类/接口/每个文件的枚举方法),如果您想构建文档,Sandcastle 会将它们分组,所以我d 更关心给他们起有意义的名字。
回答by womp
On top of keeping a consistent set of regions in your class files, I keep all components of a region in alphabetical order. I tend to have a bit of "visual memory" when it comes to reading code and it drives me crazy having to use the navigation dropdown to find code in a file because it's all over the place.
除了在类文件中保持一组一致的区域之外,我还按字母顺序保留区域的所有组件。在阅读代码时,我倾向于有一点“视觉记忆”,这让我不得不使用导航下拉菜单在文件中查找代码,这让我发疯,因为它无处不在。
回答by Scott Dorman
I use the following layout:
我使用以下布局:
events globals/class-wide fields private/internal properties methods public/protected properties methods nested classes (although I try to avoid these whenever possible)
事件全局/类范围的字段私有/内部属性方法公共/受保护属性方法嵌套类(尽管我尽可能避免使用这些)
I also firmly believe in 1 code "thing" (class, interface, or enum) per file, with the file name the same as the "thing" name. Yes, it makes a larger project but it makes it infinately easier to find things.
我也坚信每个文件有 1 个代码“事物”(类、接口或枚举),文件名与“事物”名称相同。是的,它使一个更大的项目,但它使查找事物变得更加容易。
回答by Daver
You can try Regionerate to help with this. I really like it and it's a Scott Hanselman pick.
您可以尝试使用 Regionerate 来帮助解决此问题。我真的很喜欢它,这是 Scott Hanselman 的选择。
回答by Rob Rassler
As said, I don't think there is a best way as such. But some organisation does help you the programmer.
如前所述,我认为没有最好的方法。但是某些组织确实可以帮助您成为程序员。
How often in a long project have you spent time going up and down one or more source files trying to find one of your functions.
在一个很长的项目中,您是否经常花时间在一个或多个源文件中查找您的函数之一。
So I make use of the #region
a lot to in this sort of way -
所以我#region
以这种方式利用了很多 -
region Events: All of the event references that this class uses (at least in this particular partial class).
region Controls: All functions that directly interact with controls on a form.
region MDI: set the mdi up
Then there will be some to do with functionality rather than interface,
region Regex searches
region Events:这个类使用的所有事件引用(至少在这个特定的部分类中)。
区域控件:直接与表单上的控件交互的所有功能。
区域 MDI: 设置MDI
然后将有一些与功能而不是界面有关,
区域正则表达式搜索
I sort of make it up as I go along, but using the same pattern I always use. I must say I have been told by some programmers picking up my work that it is easy to follow and others that its messy.
我一边做一边弥补,但使用我一直使用的相同模式。我必须说,一些接手我的工作的程序员告诉我,它很容易跟上,而其他人则说它很乱。
You can please half the people half the time and the other half a quarter of the time and the other quarter of the time you confuse everyone including yourself. I think Winston Chrchil said that.
你可以在一半的时间取悦一半的人,在四分之一的时间和另一半的时间里你会迷惑所有人,包括你自己。我认为 Winston Chrchil 是这么说的。