码迷,mamicode.com
首页 > Windows程序 > 详细

C# 编码标准(一)

时间:2015-01-09 00:04:04      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:

  一直想写一个自己用的代码标准,经过一段时间的优秀开源源码的观察和看其他人写的标准,感觉好的代码给人感觉就是舒服,也非常重要。所以把它们记录归纳总结,以备以后忘记,另外平时写代码的时候可以拿来参考下。下面的示例主要以Microsoft的代码为准。

命名约定

  • PascalCasing

PascalCasing 每一个单词第一个字母大写,其余字母均小写。例如:FileAccess,ArraySegment等。

除了参数、变量、常量外,所有命名空间名称、类、函数、接口、属性、事件、枚举等名称的命名,使用 Pascal 风格

  • camelCasing

camelCasing 第一个单词首字母小写,其余单词首字母大写。例如:propertyName,filePath等。

参数与变量的命名使用camelCasing.

  • SCREAMING_CAPS

SCREAMING_CAPS每个单词的所有字母都大写,单词与单词之间用"_"连接,该风格目前在c#中只用于const常量。

如:public const string DEFAULT_PAGE = "default.aspx";

  • 私有变量的命名

Private 的私有变量使用下划线"_""m_"+camelCasing的大小写规则,以便快速确认该变量的作用域。

如: private int _userId;

  private int m_userId;

一、命名约定与风格

1、类型名与方法名用pascal命名规范:

    public class StringReader : TextReader
    {
       public override String ReadToEnd()
           {
             ...
           }
   }

2、局部变量与方法参数使用camel命名规范:

   internal static String InternalCopy(String sourceFileName, String destFileName, bool checkHost) 
   {
        string fullSourceFileName = Path.GetFullPathInternal(sourceFileName);
      ...
   }

3、接口名加前缀I

   public interface IComparer
   {
         // Compares two objects. An implementation of this method must return a
         // value less than zero if x is less than y, zero if x is equal to y, or a
         // value greater than zero if x is greater than y.
         int Compare(Object x, Object y);
    }

4、私有成员变量前加前缀m_或_

  unsafe internal class PathHelper 
  {   
        // maximum size, max be greater than max path if contains escape sequence
           private int m_capacity;
           // current length (next character position)
           private int m_length;
           // max path, may be less than capacity
           private int _maxPath;
      ....
  }

5、常量使用pascal或全部大写表示:

    private const int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
    private const int FORMAT_MESSAGE_FROM_SYSTEM    = 0x00001000;
    private const int FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000;
  
    const string RsaKeyValue  = XmlSignatureConstantsNamespace + "RSAKeyValue";

 6、自定义特性类后加后缀Attribute:

  [AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true)]
    [ComVisible(true)]
    public sealed class DebuggerVisualizerAttribute: Attribute

 7、自定义异常类加后缀Exception:

  [Serializable]
    public class ArgumentException : SystemException, ISerializable

 8、方法命名使用“动词/对象”对,如 GetHashCode(...), 

  public unsafe static Array CreateInstance(Type elementType, int length)

 9、有返回值的方法名中需要描述此返回值,如GetObjectState()。

10、使用描述性变量名。

  a.避免使用单个字母作为变量名,如i或t,用indextemp来代替。

  b.避免对公共成员或受保护成员使用匈牙利标记法。

      c.避免使用缩写(如把number写成num)。

11、不要混淆使用c#中的预定义类型与CLR中的类型:

       如object 与Object, string与String,int与Int32

12、对于泛型,使用该类型的首字母代替,当使用.NET的Type类型时才使用Type作为后缀:

  public class Dictionary<K,V>

  {...}

13、使用有意见的命名空间名称,如产品名或公司名。

14、避免使用完全限定类型名。用using语句代替。

15、把所有的framework命名空间一起放在最上面,把自定义放在最下面,第三方命名空间放中间: 

using System;
using System.Collections;
using System.Collections.Generic;
using ThreadPartyLibrary;
using MyCompanyName;

16、采用委托推断,不要显示实例化委托:

delegate void SomeDelegate();
public void SomeMethod()
{...}
SomeDelegate someDelegate = SomeMethod;

17、使用Tab来进行缩进。

18、所有的成员变量都必须在顶部声明,用一行把它们与属性或方法隔开。

19、定义局部变量时,尽量使它靠近第一次使用它的地方。

20、文件名能够反应它使用的类。

21、使用左大括号({)时,换一行。

22、if下面即使只有一行代码,也要加{}包围起来:

23、为每一个命名空间建一个目录。如 MyProject.TestSuite.TestTier 使用MyProject/TestSuite/TestTier 作为路径。

24、当一行中的代码太长时,建议使用下面的方式进行断行:

  • Break after a comma----逗号后面
  • Break after an operator----操作符后面
  • Prefer higher-level breaks to lower-level breaks----
  • Align the new line with the beginning of the expression at the same level on the previous line.

   如 longMethodCall(expr1, expr2,

                 expr3, expr4, expr5);

 Examples of breaking an arithmetic expression:

 PREFER:

 var = a * b / (c - g + f) +

         4 * z;

  var = a * b / (c - g + f) +

       4 * z;

 BAD STYLE – AVOID:

 var = a * b / (c - g +

          f) + 4 * z;

25、声明变量时就进行初始化。

26、从 Stream 继承的 Framework 类型以 Stream 结尾:

  如FileStream, MemoryStream等。

27、当不理解一个方法时,可以把它们分解为更小部分,把为它们取恰当名称。

 

C# 编码标准(一)

标签:

原文地址:http://www.cnblogs.com/cang/p/4212053.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!