Giter Site home page Giter Site logo

hiram3512 / csharpnamingguidelines Goto Github PK

View Code? Open in Web Editor NEW
41.0 4.0 10.0 17 KB

C#命名规范中文版/C#编码规范中文版

csharp csharp-code csharp-script naming-conventions chinese china chinese-translation naming-convention naming-schemes guidelines

csharpnamingguidelines's Introduction

C#命名规范中文版/C#编码规范中文版

示例

/*******************************************************************
 * Description:This is a example class
 * Version: 1.0.0
 * Date: 20180218
 * Author: Hiram  
 * Email: [email protected]    
 * Copyright @ www.wikipedia.org
 *******************************************************************/

using System;

/// <summary>
/// 命名空间(Pascal)
/// </summary>
namespace Namespace
{
    /// <summary>
    /// 类(Pascal)
    /// </summary>
    public class Class
    {
        /// <summary>
        /// 属性(Pascal)
        /// </summary>
        public int Property { get; set; }

        /// <summary>
        /// 委托(Pascal):EventHandler后缀
        /// </summary>
        public delegate void EvnetHandler();

        /// <summary>
        /// 事件(Pascal):On前缀
        /// </summary>
        public event EvnetHandler OnEvent;

        /// <summary>
        /// 公有字段(Pascal)
        /// </summary>
        public readonly int Field1;

        /// <summary>
        /// 受保护字段(Pascal)
        /// </summary>
        protected int Field2;

        /// <summary>
        /// 私有字段(_Camel)
        /// </summary>
        private int _field3;
        
        /// <summary>
        /// 方法(Pascal)
        /// </summary>
        /// <param name="args">参数(Camel)</param>
        public void Method(int args)
        {
            //局部变量(Camel)
            int variable = 10;
        }
    }

    /// <summary>
    /// 接口(Pascal)
    /// </summary>
    public interface IInterface
    {
        /// <summary>
        /// 属性接口(Pascal)
        /// </summary>
        int Property { get; set; }

        /// <summary>
        /// 方法接口(Pascal)
        /// </summary>
        /// <param name="args"></param>
        void Method(int args);
    }

    /// <summary>
    /// 枚举(Pascal)
    /// </summary>
    enum Enum
    {
        Enum1,//枚举值(Pascal)
        Enum2,//枚举值(Pascal)
    }
}

参考资料


通用规则

所有的命名都是以下面两种方式进行命名:

  • PascalCasing(匈牙利命名法:每个词的第一个字母大写)
  • camelCasing(骆驼命名法:第一个词的第一个字母小写,之后每个词的第一个字母大写)
类型 命名方式 示例
Namespace Pascal namespace System.Security { ... }
Type Pascal public class StreamReader { ... }
Interface Pascal public interface IEnumerable { ... }
Method Pascal public virtual string ToString();
Property Pascal public int Length { get; }
Delegate Pascal public delegate void EvnetHandler();
Event Pascal public event EventHandler Exited;
Public Field Pascal public readonly int Min = 0;
Protected Field Pascal protected int Min = 0;
private Field Camel private int _min = 0;
Enum Pascal public enum FileMode
Parameter Camel public static int ToInt32(string value)
Local Variable Camel void Method(){int number = 10;}

特殊说明

注释

文件注释: 文件注释以如下方式进行,在扩展注释时(回车换行时)会自动添加注释行.

/*******************************************************************
* Description:This is a example class
* Version: 1.0.0
* Date: 20180218 
* Author: Hiram  
* Email: [email protected]    
* Copyright @ www.wikipedia.org
*******************************************************************/

命名空间 类型 接口 方法名 属性 事件 字段 枚举:以"///"的方式注释,编辑器会自动完善注释,并且可以用生成工具直接生成代码注释文档.

      /// <summary>
      /// 方法(Pascal)
      /// </summary>
      /// <param name="args">参数(Camel)</param>
      public void Method(int args)
      {
          //局部变量(Camel)
          int variable = 10;
      }

其他注释:以"//"的方式注释在上一行或行尾添加注释(比如局部变量,逻辑行) 段落注释:以如下方式进行

/*
       public void Method()
       {

       }
*/

待办及高亮:在注释中添加"ToDo"高亮显示注释

命名空间

类型

  • 结构体命名方式与类一致
  • 泛型默认T
  • 多个泛型根据反编译微软dll,建议命名T1,T2,T3,T4...

接口

接口定义以"I"开头,比如IInterface,IPlayer

方法

属性

属性都以Pascal方式命名

委托

微软官方建议添加以下两种后缀

  • EventHandler
  • Callback 微软官方不建议添加以下后缀
  • Delegate

事件

  • 微软官方建议:事件参数添加后缀"EventArgs"
  • 习惯命名:事件以On为前缀(比如OnClick)

字段

微软团队初期使用Camel方式命名私有字段,后来逐步采用下划线+Camel方式命名,这样做的好处是不必再识别字段是否是局部变量.

字段建议是非Public类型的,以属性替换公有的字段:这样外部访问更安全(readonly,const等除外),并且外部调用者全部使用Pascal命名方式调用对象逻辑.

微软官方只规定了public/protected以Pascal方式命名,对internal,private类型的字段没有说明,因此各种第三方规范和插件中对私有字段规范也不一致.

针对官方的示例代码,书写习惯,智能提示,代码补全和约定俗成的C#规范,建议private采用下划线+Camel方式命名,非Private字段采用Pascal方式命名.

参考微软官方命名规则,C#高级编程,Resharp命名规范,.Net源码命名规则,建议字段命名方式如下:

  • public/protected/internal以Pascal方式命名
  • private以下划线+Camel方式命名
       public readonly int Field1;
       public const int Field2 = 0;

       internal readonly int Field3;
       internal const int Field4 = 0;

       private int _field5;
       private static int _field6;
       private readonly int _field7;
       private const int _field8 = 0;
  • 以m_为前缀的方式不建议(C++命名方式)
  • 以类型为前缀的方式不建议(比如bool类型的字段以b为前缀,枚举以e为前缀)
  • 以类型为后缀的方式不建议(比如单位列表unitList,直接取名为units)

枚举

参数

局部变量

局部变量可以使用var自动声明类型

csharpnamingguidelines's People

Contributors

hiram3512 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.