C#代码规范

2019-10-20  本文已影响0人  TomGui

1.注释规范

类型、属性、委托、方法、方法参数,根据需要添加注释。
如果类型、属性、委托、方法、方法参数的名称已经是自解释了,不需要加注释;否则需要添加注释。
当添加注释时,添加方式如下所示:

namespace CodingStandards
{
    /// <summary>
    /// 产品售光时调用的委托
    /// </summary>
    public delegate void SaleOutEventHandler();

    /// <summary>
    /// 产品类,描述产品的基本信息
    /// </summary>
    public class Product
    {
        /// <summary>
        /// 产品名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 定义产品被售光时的处理逻辑
        /// </summary>
        public event SaleOutEventHandler OnSaleOut;

        /// <summary>
        /// 根据产品Id查找产品
        /// </summary>
        /// <param name="id">产品Id</param>
        /// <returns>符合此Id的产品实例,当不存在该产品时,返回null</returns>
        public Product GetProduct(int id)
        {
            //根据订单状态不同,处理不同的逻辑 
            if (flag == 0)  //当订单状态为客户提交时,….
            {
                statement;
            }
            else if (flag == 1) //当状态为已确认时,… 
            {
            }
            //根据订单状态不同,处理不同的逻辑 
            switch (flag)
            {
                case "N":  //客户提交状态
                    newFlag = 1;
                    break;
                case "I":  //传真发送状态
                    newFlag = 2;
                    break;
            }

            return null;
        }

        /// <summary>
        /// 产品类型
        /// </summary>
        public enum ProductType
        {
            出境 = 1,
            国内游 = 2
        }
    }
}

2.命名规范

2.1基本命名规范

2.2 使用Pascal风格(单词首字母大写)命名

namespace CodingStandards{}
public class Product{}
public enum ProductType
{
    出境 = 1,
    国内游 = 2
}
public Product GetProduct(int id){}
const int Value = 10;

2.3使用Camel风格(首字母小写,其后每个单词的首字母大写)命名

int name;
int personId;
public void Method(int personId){}
public class Person
{
    private string _name;
}

2.4其他命名规则

public interface ISpeak {}
public class Product
{
    public bool IsSaleOut
    {
        get { return true; }
    }

    public bool TrySale(int id)
    {
        return true;
    }

    public bool CanSale(int id)
    {
        return true;
    }
}
public delegate void WorkEventHandler ();
public event WorkEventHandler OnWork;
public abstract class AbstractPerson {}
public class LoginException {}

3.编码规范

4.布局规范

4.1用Tab作为缩进,并设置缩进大小为4

Visual Studio 2017中设置方法:菜单工具-选项-文本编辑器-C#-制表符,把制表符大小和缩进大小设置成4,点确定

4.2左右花括号必须独占一行,括号内容为空时可在一行

Visual Studio 2017 中设置方法:菜单工具-选项-文本编辑器-C#-代码样式-格式设置-新行

public void Method(int id)
{
    int i = 1;
    int j = 2;
}
public void Method(int id) {}

4.3类型成员的排列顺序

类型成员的排列顺序自上而下依次为:

/// <summary>
/// 产品类,描述产品的基本信息
/// </summary>
public class Product
{
    private int _field1;
    protected int _filed2;

    private int _property1 { get; set; }
    protected int _property2 { get; set; }
    public int _property3 { get; set; }

    private SaleOutEventHandler _event1;
    protected SaleOutEventHandler _event2;
    public SaleOutEventHandler _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; }
}

附表1:常见集合类型后缀命名

凡符合下表所列的集合类型,应添加相应的后缀

说明 后缀 示例
数组 Array int[] productArray
列表 List List<Product> productList
DataTable/HashTable Table HashTable productTable
字典 Dictionary Dictionay<string,string> productDictionary
EF中的DbSet/DataSet Set DbSet<Product> productSet

附表2:常见后缀命名

凡符合下表所列的局部变量、方法参数、字段、属性,均需添加相应的后缀

说明 后缀 示例 示例说明
费用相关 Cost ShipCost 运输费
价格相关 Price ProductUnitPrice 产品单价
消息相关 Message SuccessMessage 成功消息
日期相关 Date OrderDate 下单日期
时间相关 Time OrderTime 下单日期
计数、数量相关 Count LoginCount 登录次数
链接地址相关 Url BlogUrl 博客链接
图片相关 Image SignImage 签名图片
金额相关 Amount PrepaidAmount 预付款
点数、积分相关 Point MemberPoint 会员积分
记录、日志相关 Record ErrorRecord 错误记录
配置相关 Config DataBaseConfig 数据库配置
状态相关 Status OrderStatus 订单状态
模式、方式相关 Mode OpenMode 打开方式
种类相关 Category/Type二选一 UserCategory 用户种类
工厂类相关 Factory ConnectionFactory 连接工厂
启用相关 Enabled ExportEnabled 开启导出
流相关 Stream UploadStream 上传流
读取器相关 Reader ExcelReader Excel读取器
写入器相关 Writer ExcelWriter Excel写入器
适配器相关 Adapter IntroOPAdapter IntroOP适配器
提供器相关 Provider MemebershipProvider 会员信息提供器
包装器相关 Wrapper ProductWrapper Product包装器
连接相关 Connection ExcelConnection Excel连接
上一篇 下一篇

猜你喜欢

热点阅读