C#编码标准和命名规范
来源:互联网 发布:吉永小百合 长相 知乎 编辑:程序博客网 时间:2024/06/08 00:39
以下是C#编码标准,命名规范,还有一些最佳实践。
在你的项目里使用这些规范和(或者)调整这些适应你的需求。
类型名称和方法名称使用PascalCasing书写
public class ClientActivity{ public void ClearStatistics() { //... } public void CalculateStatistics() { //... }}
方法参数和局部变量使用camelCasing书写
public class UserLog{ public void Add(LogEvent logEvent) { int itemCount = logEvent.Items.Count; // ... }}
禁止使用匈牙利标记法或者在标识符前面增加类型标识
// 正确int counter;string name;// 避免int iCounter;string strName;
禁止使用大写标识常量或者只读型变量
// 正确public static const string ShippingType = "DropShip";// 避免public static const string SHIPPINGTYPE = "DropShip";
避免使用缩写。例外情况:常用名称的缩写,如 Id, Xml, Ftp, Uri
//正确UserGroup userGroup;Assignment employeeAssignment;// 避免UserGroup usrGrp;Assignment empAssignment;// 例外CustomerId customerId;XmlDocument xmlDocument;FtpHelper ftpHelper;UriPart uriPart;
3个或者3个以上的字母缩写使用PascalCasing(2个字母使用大写)
HtmlHelper htmlHelper;FtpTransfer ftpTransfer;UIControl uiControl;
标识符不要使用下划线,例外:私有静态变量可以使用下划线
// 正确public DateTime clientAppointment;public TimeSpan timeLeft;// 避免public DateTime client_Appointment;public TimeSpan time_Left;// 例外private DateTime _registrationDate;
使用预定义的类型名称替代系统类型名称,例如Int16, Single, UInt64等
// 正确string firstName;int lastIndex;bool isSaved;// 避免String firstName;Int32 lastIndex;Boolean isSaved;
局部变量声明使用隐式类型var。例外:主要类型((int, string, double等)使用预定义的类型。
var stream = File.Create(path);var customers = new Dictionary();// 例外int index = 100;string timeSheet;bool isCompleted;
类使用名词或者名词短语命名。
public class Employee{}public class BusinessLocation{}public class DocumentCollection{}
接口前缀使用字母I,接口名称使用名词(名词短语)或者连接词。
public interface IShape{}public interface IShapeCollection{}public interface IGroupable{}
源文件命名参照他们的主类,例外:部分类的文件名反映他们的来源或目的,例如: designer, generated等。
// Located in Task.cspublic partial class Task{ //...}// Located in Task.generated.cspublic partial class Task{ //...}
组织命名空间,命名空间要有清晰明确的结构
// Examplesnamespace Company.Product.Module.SubModulenamespace Product.Module.Componentnamespace Product.Layer.Module.Group
大括号要竖直对齐
// 正确class Program{ static void Main(string[] args) { }}
类的顶部声明成员变量,静态类型的变量在最前面
// 正确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() { // ... }}
枚举类型使用单数名称。例外:位字段枚举。
// 正确public enum Color{ Red, Green, Blue, Yellow, Magenta, Cyan}// 例外[Flags]public enum Dockings{ None = 0, Top = 1, Right = 2, Bottom = 4, Left = 8}
不要显示指定枚举的数据类型或者枚举的值(位字段枚举除外)
// 避免public enum Direction : long{ North = 1, East = 2, South = 3, West = 4}// 正确public enum Direction{ North, East, South, West}
不要在枚举名词加后缀Enum
//避免public enum CoinEnum{ Penny, Nickel, Dime, Quarter, Dollar}// 正确public enum Coin{ Penny, Nickel, Dime, Quarter, Dollar}
原文地址:
http://www.dofactory.com/reference/csharp-coding-standards
0 0
- C#编码标准和命名规范
- C#编码标准-命名规范
- C#编码标准指导和最佳实践 之 命名规范
- C#编码规范和命名规则
- C#命名规则和编码规范
- C#命名规则和编码规范
- C#编码及命名规范
- C#编码标准--命名约定和风格
- C#编码标准--命名约定和风格
- C#编码标准--命名约定和风格
- C#编码标准--命名约定和风格
- C#编码标准--命名约定和风格
- C#编码标准--命名约定和风格
- C#编码标准--命名约定和风格
- C#变量命名规范\C#编码规范:常用变量和控件在变量命
- Android 命名规范和编码规范
- Android开发命名规范和编码规范
- Android开发命名规范和编码规范
- 算法竞赛入门经典(第二版) 例题4-4 信息编码(Message Decoding) UVa213 Finals1991 (二进制)
- CSS做一个小黄人
- linux C sqlite3 mysql
- 题目:前序遍历和中序遍历树构造二叉树
- 多线程渲染(Multithreaded- rendering)3D引擎实例分析 : FlagshipEngine
- C#编码标准和命名规范
- JS的基础类型与引用类型
- 精彩依旧——2015年总结上
- 设计模式之迭代器模式
- 上传手机图片的问题
- php-log4php
- 初识IOS运用
- 关于VS2010不能创建C++项目时候的创建项目时候点不了下一步的解决办法
- 二叉树层次遍历