C#的编码规范中文版

本文详细介绍了C#编程的命名规范,包括类、方法、变量的命名规则,以及代码组织、注释、异常处理、类型使用、文件命名、接口实现等方面的最佳实践。强调了使用有意义的命名、避免硬编码、使用接口、避免显式类型转换等原则,旨在提高代码的可读性和维护性。
摘要由CSDN通过智能技术生成
1.命名规范
1. 使用 Pascal casing 定义类型和方法名
public class SomeClass
{
   public SomeMethod(){}
}
2. 使用 camel casing 定义局部变量名和方法参数
int number;
void MyMethod(int someNumber)
{}
3. 使用“I”前缀定义接口名
interface IMyInterface
{..}
4. 使用“m_”前缀定义私有成员变量
public class SomeClass
{
   private int m_Number;
}
5. 属性类使用“Attribute”为后缀
6. 异常类使用“ Exception.”为后缀
7. 方法命名采用动词短语,例如ShowDialog()
8. 带返回值的方法应该在名字中描述返回值,例如 GetObjectState()
9. 一般地,命名参数的时候加上TYPE. 这将增加代码的易读性
//正确:
public class LinkedList<KeyType,DataType>
{…}
//避免:
public class LinkedList<K,T>
{…}
10. 保持严格的缩进
  a) 使用3个空格缩进
  b) 不要使用tab键或者不标准的缩进比如1、2或者4个空格.
11. 注释的缩进和注释描述的代码同级
12. 所有的注释要拼写正确,不正确的注释意味开发混乱.
13. 使用描述性的变量名.
  a) 避免一个字符的变量名,例如“i”或者“t”。用“index”或者“temp”代替.
  b) 避免使用匈牙利命名法定义“Public”或者“protected”成员.
  c) 不要缩短单词(例如“num”代替“number”).
14. 所有的成员变量应该在顶部定义,用一个空行把它们和属性或者方法分开。
public class MyClass
{
  int m_Number;
  string m_Name;
 
  public void SomeMethod1(){}
  public void SomeMethod2(){}
}
15. 定义局部变量尽量靠近第一次使用的地方。
16. 使用有意思的命名空间,例如产品名、公司名等等
17. 避免使用类型的全称,用“using”声明代替.
18. 避免把“using”声明放在命名空间内.
19. 所有框架的命名空间在前面,自定义或者第三方的命名空间放在后面进行分组.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using MyCompmnay;
using MyControls;
20. 文件名应该反映它包含的类.
21. 总是把“{”放在新的一行.
22. 用匿名方法模拟正常方法的代码结构,排列匿名的代理
(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)





2 Coding Practices
1. 使用C#预定义的类型胜于系统空间的别名

例如:
object 而不是 Object
string 而不是 String
int 而不是 Int32
2.避免多个类放在一个文件中.
3. 一个文件应该属于一个命名空间而不是多个.
4. 避免一个文件超过500行(不包括自动产生的代码).
5. 避免方法超过25行.
6. 一行不超过80个字符.
7. 不要改动自动产生的代码.
- 5 -
©2003 IDesign Inc. All rights reserved
8. If modifying machine generated code, modify the format and style to match this
coding standard.
a) Use partial classes if possible.
9. Avoid comments that explain the obvious.
10. Code should be self explanatory. Good code with readable variable and method
names should not require comments.
11. Document only operational assumptions, algorithm insights etc.
12. Avoid method-level documentation.
a) Use extensive external documentation for API documentation.
b) Use method-level comments only as tool tips for other developers.
13. Never hard-code a numeric value, always declare a constant instead.
14. Assert every assumption.
15. On average, every fifth line is an assertion.
using System.Diagnostics;
object GetObject()
{…}
object obj = GetObject();
Debug.Assert(obj != null);
16. Make only the most necessary types public, mark others as internal.
17. Always use zero-based arrays.
18. Avoid providing explicit values for enums.
19. Avoid specifying a type for an enum (like long).
20. Never use goto unless in a switch statement fall-through.
www.idesign.net August 2003
- 6 -
©2003 IDesign Inc. All rights reserved
21. Avoid function calls in Boolean conditional statements. Assign into local variables
and check on them:
bool IsEverythingOK()
{…}
//Avoid:
if(IsEverythingOK())
{…}
//Instead:
bool ok = IsEverythingOK();
if(ok)
{…}
22. Always explicitly initialize an array of reference types using a for loop.
public class MyClass
{}
MyClass[] array = new MyClass[100];
for(int index = 0; index < array.Length; index++)
{
array[index] = new MyClass();
}
23. Only catch exceptions for which you have explicit handling.
24. In a catch statement that throws an exception, always throw the original exception to
maintain stack location of original error.
catch(Exception exception)
{
MessageBox.Show(exception.Message);
throw; //Same as throw exception;
}
25. Avoid error code as methods return values.
26. Do not use the new inheritance qualifier. Use override instead.
27. Minimize code in application assemblies (EXE client assemblies), use class libraries
instead to contain business logic.
28. Never hardcode strings that will be presented to end users. Use resources instead.
29. Never hardcode strings that might change based on deployment such as connection
strings.
30. Never use unsafe code unless when using interop.
31. Always use interfaces.
a) See Chapters 1 and 3 in Programming .NET Components.
32. Avoid multiple Main() methods in a single assembly.
www.idesign.net August 2003
- 7 -
©2003 IDesign Inc. All rights reserved
33. Avoid explicit casting. Use the as operator to defensively cast to a type.
Dog dog = new GermanShepherd();
GermanShepherd
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#(读作“See Sharp”)是一种简洁、现代、面向对象且类型安全的编程语言。 C# 起源于 C 语言家 族,因此,对于 C、 C++ 和 Java 程序员,可以很快熟悉这种新的语言。 C# 已经分别由 ECMA International 和 ISO/IEC 组织接受并确立了标准,它们分别是 ECMA-334 标准和 ISO/IEC 23270 标准。 Microsoft 用于 .NET Framework 的 C# 编译器就是根据这两个标准实现的。 C# 是面向对象的语言,然而 C# 进一步提供了对面向组件 (component-oriented) 编程的支持。现代软件 设计日益依赖于自包含和自描述功能包形式的软件组件。这种组件的关键在于,它们通过属性、方法和 事件来提供编程模型;它们具有提供了关于组件的声明性信息的特性;同时,它们还编入了自己的文 档。 C# 提供的语言构造直接支持这些概念,这使得 C# 语言自然而然成为创建和使用软件组件之选。 有助于构造健壮、持久的应用程序的若干 C# 特性:垃圾回收 (Garbage collection) 将自动回收不再使用 的对象所占用的内存;异常处理 (exception handling) 提供了结构化和可扩展的错误检测和恢复方法;类 型安全 (type-safe) 的语言设计则避免了读取未初始化的变量、数组索引超出边界或执行未经检查的类型 强制转换等情形。 C# 具有一个同一类型系统 (unified type system)。所有 C# 类型(包括诸如 int 和 double 之类的基元类 型)都继承于单个根类型: object。因此,所有类型都共享一组通用操作,并且任何类型的值都能够 以一致的方式进行存储、传递和操作。此外, C# 同时支持用户定义的引用类型和值类型,既允许对象 的动态分配,也允许轻量结构的内联存储。 为了确保 C# 程序和库能够以兼容的方式逐步演进, C# 的设计中充分强调了版本控制 (versioning)。许 多编程语言不太重视这一点,导致采用那些语言编写的程序常常因为其所依赖的库的更新而无法正常工 作。 C# 的设计在某些方面直接考虑到版本控制的需要,其中包括单独使用的 virtual 和 override 修 饰符、方法重载决策规则以及对显式接口成员声明的支持。 本章的其余部分将描述 C# 语言的基本特征。尽管后面的章节会更为详尽,有时甚至逻辑缜密地对规则 和例外情况进行描述,但本章的描述力求简洁明了,因而难免会牺牲完整性。这样做是为了向读者提供 关于该语言的概貌,一方面使读者能尽快上手编写程序,另一方面为阅读后续章节提供指导。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值