//Correct:
MyMethod(name=>MessageBox.Show(name));
//Avoid
MyMethod((name)=>{Trace.WriteLine(name);MessageBox.Show(name);});
2.Coding
2.Coding
2.Coding
2.Coding Practices
Practices
Practices
Practices 编码实践
1. Avoid putting multiple classes in a single file. 不要将多个类放到一个文件中。
2. A single file should contribute types to only a single namespace. Avoid having multiple
namespaces in the same file. 一个文件中应该只包含一个命名空间 , 不要同一文件包含多个命
名空间。
3. Avoid files with more than 500 lines (excluding machine-generated code). 避免文件大小超过
500 行,除非是机器生成的 代码 。
4. Avoid methods with more than 200 lines. 避免方法超过 200 行。
5. Avoid methods with more than 5 arguments. Use structures for passing multiple arguments. 避
免方法有超过 5 个参数,否者使用结构来传递多个参数。
6. Lines should not exceed 120 characters. 每行应该不超过 120 个字符。
7. Do not manually edit any machine-generated code. 不要手工修改机器生成的代码。
a) If modifying machine generated code, modify the format and style to match this coding
standard. 如果修改机器生成的代码,那代码风格应该遵从代码规范。
b) Use partial classes whenever possible to factor out the maintained portions. 尽量使用 partial
类来维护修改的代码。
8. Avoid comments that explain the obvious. Code should be self-explanatory. Good code with
readable variable and method names should not require comments. 不要对显而易见的逻辑增加
注释。代码应该是自解释的。好的代码使用好的变量名和方法名而不需要注释。
9. Document only operational assumptions, algorithm insights and so on. 仅对算法 、 假设等进行
文档化。
10. Avoid method-level documentation. 避免方法级的文档。
a) Use extensive external documentation for API documentation. 对 API 文档使用外部文件。
b) Use method-level comments only as tool tips for other developers. 方法级上的注释只用作给
其他开发人员的提示。
11. With the exception of zero and one, never hard-code a numeric value; always declare a
constant instead. 除了 0 和 1 外,不要硬编码任何数字,总是使用常量代替。
12. Use the const directive only on natural constants such as the number of days of the week. 只
对自然存在的数字,比如一个星期的天数等使用 const 关键字。
13. Avoid using const on read-only variables. For that, use the readonly directive. 只读的变量不
要定义为 const ,使用 readonly 关键字。
public class MyClass
{
public const int DaysInWeek = 7;