您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码:  验证码,看不清楚?请点击刷新验证码 必填



  求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Model Center   Code  
会员   
   
 
     
   
 订阅
  捐助
C#编码标准和命名规范
 
  2363  次浏览      16
 2019-8-23
 
编辑推荐:
本文来自于csdn, 本文主要介绍了C#编码标准和命名规范,并对其介绍了代码写法,希望对您能有所帮助。

以下是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等)使用预定义的类型。

Hello World!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.cs
public partial class Task
{
//...
}

// Located in Task.generated.cs
public partial class Task
{
//...
}

组织命名空间,命名空间要有清晰明确的结构

// Examples
namespace Company.Product.Module.SubModule
namespace Product.Module.Component
namespace 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
}
   
2363 次浏览       16
相关文章

深度解析:清理烂代码
如何编写出拥抱变化的代码
重构-使代码更简洁优美
团队项目开发"编码规范"系列文章
相关文档

重构-改善既有代码的设计
软件重构v2
代码整洁之道
高质量编程规范
相关课程

基于HTML5客户端、Web端的应用开发
HTML 5+CSS 开发
嵌入式C高质量编程
C++高级编程