好的代码应该拥有以下几个特征:
代码容易编写,并易于修改和扩展。
代码干净,并表述准确。
代码有价值,并注重质量。
符合微软规范并易读
1. 类名和方法名使用 PascalCasing.
public class ClientActivity
{
public void ClearStatistics()
{
//...
}
public void CalculateStatistics()
{
//...
}
}
2. 方法参数和局部变量使用 camelCasing
public class UserLog
{
public void Add(LogEvent logEvent)
{
int itemCount = logEvent.Items.Count;
// ...
}
}
3. 不要使用Hungarian notation或者其他类型标识在标识符
// Correct
int counter;
string name;
// Avoid
int iCounter;
string strName;
4. 不要对常量和只读变量使用Screaming Caps
// Correct
public static const string ShippingType = "DropShip";
// Avoid
public static const string SHIPPINGTYPE = "DropShip";
5. 避免使用缩写.例外: abbreviations commonly used as names, such as Id, Xml, Ftp, Uri
// Correct
UserGroup userGroup;
Assignment employeeAssignment;
// Avoid
UserGroup usrGrp;
Assignment empAssignment;
// Exceptions
CustomerId customerId;
XmlDocument xmlDocument;
FtpHelper ftpHelper;
UriPart uriPart;
6. 缩写3个以上字符 (2 chars are both uppercase)使用PascalCasing
HtmlHelper htmlHelper;
FtpTransfer ftpTransfer;
UIControl uiControl;
7. 不要使用在标识符使用下划线. 例外: you can prefix private static variables
with an underscore.
// Correct
public DateTime clientAppointment;
public TimeSpan timeLeft;
// Avoid
public DateTime client_Appointment;
public TimeSpan time_Left;
// Exception
private DateTime _registrationDate;
8. 使用预定义类型名而不是系统类型名如 Int16, Single, UInt64, 等
// Correct
string firstName;
int lastIndex;
bool isSaved;
// Avoid
String firstName;
Int32 lastIndex;
Boolean isSaved;
9. 局部变量声明使用隐式类型 var. 例外Exception: primitive types (int, string,
double, etc) use predefined names.
var stream = File.Create(path);
var customers = new Dictionary();
// Exceptions
int index = 100;
string timeSheet;
bool isCompleted;
10. 使用名称或名词短语来命名类.
public class Employee
{
}
public class BusinessLocation
{
}
public class DocumentCollection
{
}
11. 接口前缀带I. 接口名是名词(短语)或者形容词.
public interface IShape
{
}
public interface IShapeCollection
{
}
public interface IGroupable
{
}
12. 根据主类命名来源文件. 例外: file names with partial classes
reflect their source or purpose, e.g. designer, generated, etc.
// Located in Task.cs
public partial class Task
{
//...
}
// Located in Task.generated.cs
public partial class Task
{
//...
}
13. 清晰的定义结构来组织命名空间
// Examples
namespace Company.Product.Module.SubModule
namespace Product.Module.Component
namespace Product.Layer.Module.Group
14. 花括号要竖向排列
// Correct
class Program
{
static void Main(string[] args)
{
}
}
15.在类头部声明所有的成员变量, 静态变量在最头部.
// Correct
public class Account
{
public static string BankName;
public static decimal Reserves;
public string Number {get; set;}
public DateTime DateOpened {get; set;}
public DateTime DateClosed {get; set;}
public decimal Balance {get; set;}
// Constructor
public Account()
{
// ...
}
}
16. 使用单数命名枚举. 例外: bit field enums.
// Correct
public enum Color
{
Red,
Green,
Blue,
Yellow,
Magenta,
Cyan
}
// Exception
[Flags]
public enum Dockings
{
None = 0,
Top = 1,
Right = 2,
Bottom = 4,
Left = 8
}
17. 不要显式指定枚举的类型或者枚举的值(except bit fields)
// Don‘t
public enum Direction : long
{
North = 1,
East = 2,
South = 3,
West = 4
}
// Correct
public enum Direction
{
North,
East,
South,
West
}
18. 不要用Enum做后缀命名枚举
// Don‘t
public enum CoinEnum
{
Penny,
Nickel,
Dime,
Quarter,
Dollar
}
// Correct
public enum Coin
{
Penny,
Nickel,
Dime,
Quarter,
Dollar
}
原文链接:http://www.dofactory.com/reference/csharp-coding-standards
补充
1. 类型名和源文件名词一致
如类型名Product,源文件命名Product.cs
2.方法的命名。一般将其命名为动宾短语。
public class File
{
public void CreateFile(string filePath)
{
}
public void GetPath(string path)
{
}
}
3、类型成员的排列顺序
类型成员的排列顺序自上而下依次为:
字段:私有字段、受保护字段
属性:私有属性、受保护属性、公有属性
事件:私有事件、受保护事件、公有事件
构造函数:参数数量最多的构造函数,参数数量中等的构造函数,参数数量最少的构造函数
方法:重载方法的排列顺序与构造函数相同,从参数数量最多往下至参数最少
Public class Product
{
private int field1;
protect int field2;
privite int property1{get;set;}
protect int property2{get;set;}
Public int Property3{get;set;}
private SalesOutEventHander event1;
protect SalesOutEventHander event2;
public SalesOutEventHander Event3;
public Product(int param1, int param2)
{
}
public Product(int param1)
{
}
public Product()
{
}
public Product GetProduct(int id,string area)
{
return null;
}
public Product GetProduct(int id)
{
return null;
}
public Product GetProduct()
{
return null;
}
}
4、委托和事件的命名
委托以EventHandler作为后缀命名,例如 SalesOutEventHandler。
事件以其对应的委托类型,去掉EventHandler后缀,并加上On前缀构成。
public delegate void SalesOutEventHander();
public class Product
{
public SalesOutEventHander OnSalesOut;
}
5、返回bool类型的方法、属性的命名
如果方法返回的类型是bool类型,则其前缀为Is,例如:IsHidden。
如果某个属性的类型为bool类型,则其前缀为Can,例如:CanHidden。
6、常见集合后缀类型命名
凡符合下表所列的集合类型,应添加相应的后缀。
Array int[] productArray
List List<Product> productList
Table HashTable productTable
Dictionary Dictionary<string,string> productDictionary
Set DbSet<Product> productSet
7、常见字段、属性命名
字段、属性种类比较繁杂,因此仅列出最常用的几项
Id GuidId Name Title Remark Category Linkman
原文链接:http://www.cnblogs.com/JimmyZhang/archive/2013/06/05/3118936.html
原文:http://www.cnblogs.com/dennysong/p/5437361.html