日期:2009-01-28  浏览次数:20494 次

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