C#代码常用18大注意点
作者:dofactory.com 来源:dofactory.com 发布时间:2015-07-03 查看数:11680
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(匈牙利)命名法或者其它类型标识来标记变量(控件除外,如: txtUser)。
// Correct
int counter;
string name;
// Avoid
int iCounter;
string strName;
4、不要使用全大写字母来做常量或只读变量的命名
// Correct
public static const string ShippingType = "DropShip";
// Avoid
public static const string SHIPPINGTYPE = "DropShip";
5、避免使用缩写,但是常用的缩写例外,比如Id,Xml,Ftp,Uri
// Correct
UserGroup userGroup;
Assignment employeeAssignment;
// Avoid
UserGroup usrGrp;
Assignment empAssignment;
// Exceptions
CustomerId customerId;
XmlDocument xmlDocument;
FtpHelper ftpHelper;
UriPart uriPart;
6、使用camelCasing使名变量时,如果存在二个或以上大写字母的组合时,应将其余的字母 改为小写
HtmlHelper htmlHelper;
FtpTransfer ftpTransfer;
UIControl uiControl;
7、不要在标识符中使用下划线,但是私有变量的命名可以使用下划线做前缀。
// Correct
public DateTime clientAppointment;
public TimeSpan timeLeft;
// Avoid
public DateTime client_Appointment;
public TimeSpan time_Left;
// Exception
private DateTime _registrationDate;
8、使用预定义的类型名称
// Correct
string firstName;
int lastIndex;
bool isSaved;
// Avoid
String firstName;
Int32 lastIndex;
Boolean isSaved;
9、使用var来定义局部变量,原始类型(int,string,bool等)例外
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、根据主要类别或作用命名类名 // Located in Task.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、使用单数形式命名枚举,但是位域枚举列外
// 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、不要明确指定枚举类型的值,位域枚举列外
// 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 本站翻译