IDesign发布了C#编程规范,小鸡射手从Only4Gurus下载浏览后决心抽时间翻译一下,以更好地学习。
目录内容如下:
1 命名规则和风格 Naming Conventions and Style
2 编码惯例 Coding Practices
3 项目设置和结构 Project Settings and Structure
4 Framework特别指导 Framework Specific Guidelines
4.1 数据访问 Data Access
4.2 ASP.NET和Web Service ASP.NET and Web Services
4.3 序列化 Serialization
4.4 多线程 Multithreading
4.5 Remoting Remoting
4.6 安全 Security
4.7 服务组件 Enterprise Services
5 资源 Resources
今天只翻译了命名规则部分,译文及原文对照如下,其中的tip是附加的,:-)
命名规则和风格
Naming Conventions and Style
1. 类和方法名采用Pascal风格
Use Pascal casing for type and method names
public class SomeClass
{
public SomeMethod(){}
}
2. 局部变量和方法参数采用camel风格
Use camel casing for local variable names and method arguments
int number;
void MyMethod(int someNumber)
{}
3. 接口名采用I作为前缀
Prefix interface name with I
interface IMyInterface
{..}
4. 私有成员变量采用m_作为前缀
Prefix private member variables with m_
public class SomeClass
{
private int m_Number;
}
5. 自定义属性类名采用Attribute作为后缀
Suffix custom attribute classes with Attribute.
6. 自定义异常类名采用Exception作为后缀
Suffix custom exception classes with Exception.
7. 采用动词-对象对命名方法,例如ShowDialog()
Name methods using verb-object pair, such as ShowDialog()
8. 有返回值的方法应该取名表示其返回值,例如GetObjectState()
Methods with return values should have a name describing the value returned, such as GetObjectState().
9. 采用描述性的变量名。
Use descriptive variable names.
a) 避免采用单字母的变量名,如i或t;而是采用index或temp。
Avoid single character variable names, such as i or t. Use index or temp instead.
b) 对public和protected成员避免采用用匈牙利命名法。
Avoid using Hungarian notation for public or protected members.
c) 不要采用缩写(例如将number缩写为num)。
Do not abbreviate words (such as num instead of number).
10. 总是使用C#预定义的类型,而不是使用System命名空间中的别名。例如:采用object不用Object,采用string不用String,采用int不用Int32。
Always use C# predefined types rather than the aliases in the System namespace.
For example:
object NOT Object
string NOT String
int NOT Int32
11. 对于泛型,类型采用大写字母。当处理.NET类型Type时保留后缀Type。
With generics, use capital letters for types. Reserve suffixing Type when dealing with the .NET type Type.
// 正确:
//Correct:
public class LinkedList
// 避免使用:
//Avoid:
public class LinkedList
12. 采用有意义的命名空间名,例如产品名称或公司名称。
Use meaningful namespaces such as the product name or the company name.
13. 避免使用类的全称,而是采用using语句。
Avoid fully qualified type names. Use the using statement instead.
14. 避免在命名空间内使用using语句。
Avoid putting a using statement inside a namespace.
15. 将所有framework命名空间名放在一起,后面放自定义或第三方的命名空间名。
Group all framework namespaces together and put custom or third party namespaces underneath.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using MyCompany;
using MyControls;
16. 采用委托推断,不要显式实例化委托。
Use delegate inference instead of explicit delegate instantiation
delegate void SomeDelegate();
public void SomeMethod()
{}
SomeDelegate someDelegate = SomeMethod;
17. 严格遵守缩进格式。
Maintain strict indentation.
a) 缩进采用3个空格。
Use 3 spaces for indentation.
b) 不用采用tab或非标准的缩进,如1、2或4个空格。
Do not use tabs or non-standard indentation like 1, 2 or 4 spaces.
18. 注释缩进和其注释的代码在同一层次。
Indent comment at the same level of indentation as the code you are documenting.
19. 所有注释要经过拼写检查。拼写错误的注释表明开发的草率。
All comments should pass spell checking. Misspelled comments indicate sloppy development.
20. 所有成员变量应该定义在前面,和属性或方法间空开一行。
All member variables should be declared at the top, with one line separating them from the properties or methods.
public class MyClass
{
int m_Number;
string m_Name;
public void SomeMethod1()
{}
public void SomeMethod2()
{}
}
21. 局部变量的定义尽可能靠近它的初次使用。
Declare a local variable as close as possible to its first use.
22. 文件名应该体现其包含的类。
A file name should reflect the class it contains.
23. 当使用partial类型且每部分分配一个文件时,以类型名加P和序数命名每个文件。
When using partial types and allocating a part per file, name each file after the type suffixed with a P and an ordinal number:
//In MyClassP1.cs
public partial class MyClass
{}
//In MyClassP2.cs
public partial class MyClass
{}
24. 左大括号总是放在新行中。
Always place an open curly brace ({) in a new line.
25. 匿名方法模仿普通方法的布局,和匿名委托定义放在一行。
With anonymous methods mimic the code layout of a regular method, aligned with the anonymous delegate declaration.
a) 遵守将左大括号放在新行的规则。
Comply with placing an open curly brace in a new line
delegate void SomeDelegate(string someString);
//正确
//Correct:
public void InvokeMethod()
{
SomeDelegate someDelegate = delegate(string name)
{
MessageBox.Show(name);
};
someDelegate("Juval");
}
//避免采用:
//Avoid
public void InvokeMethod()
{
SomeDelegate someDelegate = delegate(string name){MessageBox.Show(name);};
someDelegate("Juval");
}
26. 没有参数的匿名方法使用空括号。
Use empty parenthesis on parameter-less anonymous methods
a) 仅当匿名方法可能被用于任何委托时省略括号。
Omit the parenthesis only if the anonymous method could have been used on any delegate.
delegate void SomeDelegate();
//Correct
SomeDelegate someDelegate1 = delegate()
{
MessageBox.Show("Hello");
};
//Avoid
SomeDelegate someDelegate1 = delegate
{
MessageBox.Show("Hello");
};
……