C# 命名规则和编码规范

Pascal 规则:每个单词开头的字母大写。如:TestCounter
Camel 规则:除了第一个单词外的其他单词的开头字母大写。如:testCounter
Upper 规则:大写所有字母。仅用于一两个字符长的常量的缩写命名,超过三个字符长度应该应用Pascal规则

public class Math
{
    public const PI = ...;
    public const E = ...;
    public const FeigenBaumNumber = ...;
}
  1. 用Pascal规则来命名属性、方法、事件和类名
public class ClientActivity
{
    public void ClearStatistics()
    {
        //...
    }
    public void CalculateStatistics()
    {
        //...
    }
}
  1. 自定义的属性以Attribute结尾
public class TableAttribute : Attribute { }
  1. 自定义的异常以Exception结尾
public class NullEmptyException : Exception
{
    // ...
}
  1. 方法的命名。一般将其命名为动宾短语
public class File
{
    public void CreateFile(string filePath)
    { }
    public void GetPath(string path)
    { }
}
  1. 用Camel规则来命名成员变量、局部变量和方法的参数
public class UserLog
{
    public void Add(LogEvent logEvent)
    {
        int itemCount = logEvent.Items.Count;
        // ...
    }
}
  1. 不要使用匈牙利命名法。不要给成员变量加任何前缀(如_、m_、s_等等)。
    如果想要区分局部变量和成员变量,可以使用this关键字
// Correct
int counter;
string name;
// Avoid
int iCounter;
string strName;
  1. 不要将常量或者只读变量的变量名全部大写,而使用Pascal规则来命名。
// Correct
public static const string ShippingType = "DropShip";
// Avoid
public static const string SHIPPINGTYPE = "DropShip";
  1. 避免使用缩写。 例外:通常用作名称的缩写,例如Id,Xml,Ftp,Uri
// Correct
UserGroup userGroup;
Assignment employeeAssignment;
// Avoid
UserGroup usrGrp;
Assignment empAssignment;
// Exceptions
CustomerId customerId;
XmlDocument xmlDocument;
FtpHelper ftpHelper;
UriPart uriPart;
  1. 变量名是一个单词的尽量不要缩写,多单词组成的变量名可适当缩写。

  2. 使用PascalCasing缩写3个字符或更多(2个字符都是大写)

HtmlHelper htmlHelper;
FtpTransfer ftpTransfer;
UIControl uiControl;
  1. 局部变量的名称要有意义。不要直接用用i,j,k,l,m,n,x,y,z等做变量名,但for循环除外
for (int i = 0; i < 100000; i++)
{
    // ...
}
  1. 布尔型变量或者方法一般可以用is、can或者has做前缀。
bool isFinished = true;
bool canWork = true;
  1. 判断条件是一个布尔变量时不要使用 == 进行条件判断。
// 不友好的写法
private bool isFinished = true;
if(isFinished == true)
{
    // ...
}
// 正确的写法
private bool isFinished = true;
if(isFinished)
{
    // ...
}
  1. 不要在标识符中使用下划线。例外:您可以为私有静态变量添加前缀用下划线。
// Correct
public DateTime clientAppointment;
public TimeSpan timeLeft;
// Avoid
public DateTime client_Appointment;
public TimeSpan time_Left;
// Exception
private DateTime _registrationDate;
  1. 使用预定义的类型名称而不是系统类型名称,如Int16,Single,UInt64等
// Correct
string firstName;
int lastIndex;
bool isSaved;
// Avoid
String firstName;
Int32 lastIndex;
Boolean isSaved;
  1. 将隐式类型var用于局部变量声明。 例外:原始类型(int,string,double等)使用预定义的名称。
var stream = File.Create(path);
var customers = new Dictionary();
// Exceptions
int index = 100;
string timeSheet;
bool isCompleted;
  1. 使用名词或名词短语来命名一个类。
public class Employee
{ 
    //...
}
public class BusinessLocation
{
    //...
}
public class DocumentCollection
{
    //...
}
  1. 前缀接口与字母I.接口名称是名词(短语)或形容词。
public interface IShape
{
    //...
}
public interface IShapeCollection
{
    //...
}
public interface IGroupable
{
    //...
}
  1. 根据主类命名源文件。 例外:具有部分类的文件名反映他们的来源或目的,例如 设计师,生成等
// Located in Task.cs
public partial class Task
{
    //...
}
// Located in Task.generated.cs
public partial class Task
{
    //...
}
  1. 组织具有明确定义结构的名称空间
// Examples
namespace Company.Product.Module.SubModule
namespace Product.Module.Component
namespace Product.Layer.Module.Group
  1. 垂直对齐花括号。
// Correct
class Program
{
    static void Main(string[] args)
    {
    }
}
  1. 将所有成员变量声明在类的顶部,静态变量位于最顶层。
// 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()
    {
        // ...
    }
}
  1. 为枚举使用单数名称。 例外:位字段枚举。
// 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
}
  1. 不要显式指定枚举的类型或枚举的值(位域除外)
// Don't
public enum Direction : long
{
    North = 1,
    East = 2,
    South = 3,
    West = 4
}
// Correct
public enum Direction
{
    North,
    East,
    South,
    West
}
  1. 不要使用Enum后缀作为枚举名称
// Don't
public enum CoinEnum
{
    Penny,
    Nickel,
    Dime,
    Quarter,
    Dollar
}
// Correct
public enum Coin
{
    Penny,
    Nickel,
    Dime,
    Quarter,
    Dollar
}
  1. 可以用缩写作为UI元素的前缀。常见UI组件的一般缩写形式:
Label --> labl
TextBox --> txt
Button --> btn 
Image --> img 
Widget --> Wgt
List --> lst
CheckBox --> chk 
Hyperlink --> lnk
Panel --> pnl
Table --> tab 
ImageButton --> imb
posted @   lqqgis  阅读(706)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示