C# Coding Conventions(译)


MSDN

C# Coding Conventions C#编码规范

The C# Language Specification does not define a coding standard. However, the guidelines in this topic are used by Microsoft to develop samples and documentation.

C#语言规范不是用来规定编码标准。然而,这些规范都是微软在开发示例和文档时遵循的。

Coding conventions serve the following purposes:

编码规范是为了达成下列目标:

  • They create a consistent look to the code, so that readers can focus on content, not layout.

    保证代码外观的一致性,让读者讲精力集中在内容上,而不是形式。

  • They enable readers to understand the code more quickly by making assumptions based on previous experience.

    通过良好的命名规范,使读者可以结合过往经验快速理解代码。

  • They facilitate copying, changing, and maintaining the code.

    通过规范简化复制、更改和维护代码。

  • They demonstrate C# best practices.

    演示C#最佳实践。

Naming Conventions 命名规范


  • In short examples that do not include using directives, use namespace qualifications. If you know that a namespace is imported by default in a project, you do not have to fully qualify the names from that namespace. Qualified names can be broken after a dot (.) if they are too long for a single line, as shown in the following example.

    在不包括using指令的短示例中,使用命名空间限定。如果你知道命名空间默认导入项目中,则不必完全限定来自该命名空间的名称。 如果命名空间限定名太长,则可以在点 (.) 后中断限定名称,如下例所示。

  1. var currentPerformanceCounterCategory =newSystem.Diagnostics.
  2. PerformanceCounterCategory();
  • You do not have to change the names of objects that were created by using the Visual Studio designer tools to make them fit other guidelines.

    你不必通过更改 Visual Studio 设计器工具创建的对象的名称以使它们适合其他准则。

Layout Conventions 布局规范


Good layout uses formatting to emphasize the structure of your code and to make the code easier to read. Microsoft examples and samples conform to the following conventions:

好的代码布局用格式来强调代码的结构并使代码易于阅读。 Microsoft 示例和样本遵循以下规范:

  • Use the default Code Editor settings (smart indenting, four-character indents, tabs saved as spaces). For more information, see Options, Text Editor, C#, Formatting.

    使用默认的代码编辑器设置(智能缩进、4 字符缩进、制表符保存为空格)。

  • Write only one statement per line.

    每行只写一条语句。

  • Write only one declaration per line.

    每行只写一个声明。

  • If continuation lines are not indented automatically, indent them one tab stop (four spaces).

    如果连续行未自动缩进,则缩进一个tab的宽度(4空格)。

  • Add at least one blank line between method definitions and property definitions.

    每个方法定义和每个属性定义间至少间隔一个空白行。

  • Use parentheses to make clauses in an expression apparent, as shown in the following code.

    使用()号让语句更清晰明了,如下代码所示。

  1. if((val1 > val2)&&(val1 > val3))
  2. {
  3.    // Take appropriate action.
  4. }

Commenting Conventions 注释规范


  • Place the comment on a separate line, not at the end of a line of code.

    将注释放在单独的行上,而非代码行的末尾。

  • Begin comment text with an uppercase letter.

    注释以大写字母开头。

  • End comment text with a period.

    注释以句号结尾。

  • Insert one space between the comment delimiter (//) and the comment text, as shown in the following example.

    注释分隔符//与注释文本间空一个空格,如下例所示。

  1. // The following declaration creates a query. It does not run
  2. // the query.
  • Do not create formatted blocks of asterisks around comments.

    不要在注释周围创建格式化的星号块。

Language Guidelines 语言准则


The following sections describe practices that the C# team follows to prepare code examples and samples.

以下各节介绍 C# 团队编写示例和样本时遵循的做法。

String Data Type String数据类型

  • Use the + operator to concatenate short strings, as shown in the following code.

    使用“+”运算符来连接短字符串,如下代码所示。

  1. string displayName = nameList[n].LastName+", "+ nameList[n].FirstName;
  • To append strings in loops, especially when you are working with large amounts of text, use a StringBuilder object.

    在循环中拼接字符串,尤其是有大量字符串时,请使用StringBuilder对象。

  1. var phrase ="lalalalalalalalalalalalalalalalalalalalalalalalalalalalalala";
  2. var manyPhrases =newStringBuilder();
  3. for(var i =0; i <10000; i++)
  4. {
  5.   manyPhrases.Append(phrase);    
  6. }
  7. //Console.WriteLine("tra" + manyPhrases);

Implicitly Typed Local Variables 隐式类型的局部变量

  • Use implicit typing for local variables when the type of the variable is obvious from the right side of the assignment, or when the precise type is not important.

    当变量类型明显来自赋值的右侧时,或者当具体类型不重要时,请对本地变量进行隐式类型化。

  1. // When the type of a variable is clear from the context, use var
  2. // in the declaration.
  3. var var1 ="This is clearly a string.";
  4. var var2 =27;
  5. var var3 =Convert.ToInt32(Console.ReadLine());
  • Do not use var when the type is not apparent from the right side of the assignment.

    当变量并非明显来自赋值右侧时,请勿使用var。

  1. // When the type of a variable is not clear from the context, use an
  2. // explicit type.
  3. int var4 =ExampleClass.ResultSoFar();
  • Do not rely on the variable name to specify the type of the variable. It might not be correct.

    不用依靠变量名来判断变量类型。这可能是错误的。

  1. // Naming the following variable inputInt is misleading.
  2. // It is a string.
  3. var inputInt =Console.ReadLine();
  4. Console.WriteLine(inputInt);
  • Avoid the use of var in place of dynamic.

    避免用var来代替dynamic

  • Use implicit typing to determine the type of the loop variable in for and foreach loops.

    for和foreach循环中推荐使用var。

The following example uses implicit typing in a for statement.

下例for循环中使用隐式类型化。

  1. var syllable ="ha";
  2. var laugh ="";
  3. for(var i =0; i <10; i++)
  4. {
  5.   laugh += syllable;
  6.   Console.WriteLine(laugh);
  7. }

The following example uses implicit typing in a foreach statement.

下例foreach循环中使用隐式类型化。

  1. foreach(var ch in laugh)
  2. {
  3.   if(ch =='h')
  4.     Console.Write("H");
  5.   else
  6.     Console.Write(ch);
  7. }
  8. Console.WriteLine();

Unsigned Data Type 无符号类型

  • In general, use int rather than unsigned types. The use of int is common throughout C#, and it is easier to interact with other libraries when you use int.

    通常,使用 int 而不是无符号类型。 int 的使用在整个 C# 中都很常见,并且当你使用 int 时,更易于与其他库交互。

Arrays 数组

  • Use the concise syntax when you initialize arrays on the declaration line.

    使用简洁的语法来声明数组。

  1. // Preferred syntax. Note that you cannot use var here instead of string[].
  2. string[] vowels1 ={"a","e","i","o","u"};
  3. // If you use explicit instantiation, you can use var.
  4. var vowels2 =new string[]{"a","e","i","o","u"};
  5. // If you specify an array size, you must initialize the elements one at a time.
  6. var vowels3 =new string[5];
  7. vowels3[0]="a";
  8. vowels3[1]="e";
  9. // And so on.

Delegates 委托

  • Use the concise syntax to create instances of a delegate type.

    使用简洁的语法来创建委托实例。

  1. // First, in class Program, define the delegate type and a method that
  2. // has a matching signature.
  3. // Define the type.
  4. public delegate void Del(string message);
  5. // Define a method that has a matching signature.
  6. public static void DelMethod(string str)
  7. {
  8.   Console.WriteLine("DelMethod argument: {0}", str);
  9. }
  1. // In the Main method, create an instance of Del.
  2. // Preferred: Create an instance of Del by using condensed syntax.
  3. Del exampleDel2 =DelMethod;
  4. // The following declaration uses the full syntax.
  5. Del exampleDel1 =new Del(DelMethod);

try-catch and using Statements in Exception Handling 异常处理中的 try-catch 和 using 语句

  • Use a try-catch statement for most exception handling.

    对大多数异常处理使用 try-catch 语句。

  1. staticstringGetValueFromArray(string[] array,int index)
  2. {
  3.   try
  4.   {
  5.     return array[index];
  6.   }
  7.   catch(System.IndexOutOfRangeException ex)
  8.   {
  9.     Console.WriteLine("Index is out of range: {0}", index);
  10.     throw;
  11.   }
  12. }
  • Simplify your code by using the C# using statement. If you have a try-finally statement in which the only code in the finally block is a call to theDispose method, use a using statement instead.

    如果你异常处理 finally 块中的唯一代码是调用 Dispose 方法,请改用 using。

  1. // This try-finally statement only calls Dispose in the finally block.
  2. Font font1 =newFont("Arial",10.0f);
  3. try
  4. {
  5.   byte charset = font1.GdiCharSet;
  6. }
  7. finally
  8. {
  9.   if(font1 !=null)
  10.   {
  11.     ((IDisposable)font1).Dispose();
  12.   }
  13. }
  14. // You can do the same thing with a using statement.
  15. using(Font font2 =newFont("Arial",10.0f))
  16. {
  17.   byte charset = font2.GdiCharSet;
  18. }

&& and || Operators

  • To avoid exceptions and increase performance by skipping unnecessary comparisons, use && instead of & and || instead of | when you perform comparisons, as shown in the following example.

    若要通过跳过不必要的比较来避免异常或提高性能,请在执行比较时使用 && 来代替 &,使用 || 来代替 | ,如下例所示。

  1. Console.Write("Enter a dividend: ");
  2. var dividend =Convert.ToInt32(Console.ReadLine());
  3. Console.Write("Enter a divisor: ");
  4. var divisor =Convert.ToInt32(Console.ReadLine());
  5. // If the divisor is 0, the second clause in the following condition
  6. // causes a run-time error. The && operator short circuits when the
  7. // first expression is false. That is, it does not evaluate the
  8. // second expression. The & operator evaluates both, and causes
  9. // a run-time error when divisor is 0.
  10. if((divisor !=0)&&(dividend / divisor >0))
  11. {
  12.   Console.WriteLine("Quotient: {0}", dividend / divisor);
  13. }
  14. else
  15. {
  16.   Console.WriteLine("Attempted division by 0 ends up here.");
  17. }

New Operator New 运算符

  • Use the concise form of object instantiation, with implicit typing, as shown in the following declaration.

    使用简洁的形式来实例化隐式类型,如下声明所示。

  1. var instance1 =newExampleClass();

The previous line is equivalent to the following declaration.

上一行等同于下面的声明。

  1. ExampleClass instance2 =newExampleClass();
  • Use object initializers to simplify object creation.

    使用对象初始化器简化对象的创建。

  1. // Object initializer.
  2. var instance3 =newExampleClass{Name="Desktop", ID =37414,
  3. Location="Redmond",Age=2.3};
  4. // Default constructor and assignment statements.
  5. var instance4 =newExampleClass();
  6. instance4.Name="Desktop";
  7. instance4.ID =37414;
  8. instance4.Location="Redmond";
  9. instance4.Age=2.3;

Event Handling 事件处理

  • If you are defining an event handler that you do not need to remove later, use a lambda expression.

    如果你正在定义一个你稍后不会删除的事件处理程序,请使用Lambda表达式。

  1. publicForm2()
  2. {
  3.   // You can use a lambda expression to define an event handler.
  4.   this.Click+=(s, e)=>
  5.   {
  6.     MessageBox.Show(
  7.       ((MouseEventArgs)e).Location.ToString());
  8.   };
  9. }
  1. // Using a lambda expression shortens the following traditional definition.
  2. publicForm1()
  3. {
  4.   this.Click+=newEventHandler(Form1_Click);
  5. }
  6. voidForm1_Click(object sender,EventArgs e)
  7. {
  8.   MessageBox.Show(((MouseEventArgs)e).Location.ToString());
  9. }

Static Members 静态成员

  • Call static members by using the class name: ClassName.StaticMember. This practice makes code more readable by making static access clear. Do not qualify a static member defined in a base class with the name of a derived class. While that code compiles, the code readability is misleading, and the code may break in the future if you add a static member with the same name to the derived class.

    使用类名来调用静态成员;类名.静态成员名。这种做法通过指明静态访问使代码更易读。请勿使用派生类名来限定基类中定义的静态成员。当那样的代码编译时,代码的可读性会造成误导,若将来你在派生类添加了同名的静态成员,代码可能会崩溃。

LINQ Queries LINQ查询

  • Use meaningful names for query variables. The following example uses seattleCustomers for customers who are located in Seattle.

    查询变量使用有意义的名称。下面示例中使用seattleCustomers来表示住在Seattle(西雅图)的客户。

  1. var seattleCustomers =
  2.             from cust in customers
  3.             where cust.City=="Seattle"
  4.             select cust.Name;
  • Use aliases to make sure that property names of anonymous types are correctly capitalized, using Pascal casing.

    使用别名来确保匿名类型的属性名都是Pascal命名法。

    • Pascal(帕斯卡): 所有单词首字母大写。例如 WriteLine
    • Camel(驼峰式): 第一个单词首字母小写,其他单词首字母大写。例如 secondField
  1. var localDistributors =
  2.            from customer in customers
  3.            join distributor in distributors on customer.City equals distributor.City
  4.            select new{Customer= customer,Distributor= distributor };
  • Rename properties when the property names in the result would be ambiguous. For example, if your query returns a customer name and a distributor ID, instead of leaving them as Name and ID in the result, rename them to clarify that Name is the name of a customer, and ID is the ID of a distributor.

    如果结果中的属性名模棱两可,则对其重命名。例如,如果你的查询返回了客户名称和分销商ID,在返回结果不要将它们存为NameID,而是应该重命名以明确Name指的是客户的名称,ID指的是分销商的ID。

  1. var localDistributors2 =
  2.            from cust in customers
  3.            join dist in distributors on cust.City equals dist.City
  4.            select new{CustomerName= cust.Name,DistributorID= dist.ID };
  • Use implicit typing in the declaration of query variables and range variables.

    声明查询变量和范围变量时使用隐式类型。

  1. var seattleCustomers =
  2.            from cust in customers
  3.            where cust.City=="Seattle"
  4.            select cust.Name;
  • Align query clauses under the from clause, as shown in the previous examples.

    对其查询语句和from子句,如上面的几个例子所示。

  • Use where clauses before other query clauses to ensure that later query clauses operate on the reduced, filtered set of data.

    在别的查询语句之前使用where子句,来确保后面查询的是筛选后的数据集。

  1. var seattleCustomers2 =
  2.                from cust in customers
  3.                where cust.City=="Seattle"
  4.                orderby cust.Name
  5.                select cust;
  • Use multiple from clauses instead of a join clause to access inner collections. For example, a collection of Student objects might each contain a collection of test scores. When the following query is executed, it returns each score that is over 90, along with the last name of the student who received the score.

    使用多行from子句来代替Join子句访问内部集合。例如,一个Student集合里,可能每个学生都有一个考试分数集合。当执行下面的查询时,它将返回90分以上的成绩,并返回得到该分数的学生的姓氏。

  1. // Use a compound from to access the inner sequence within each element.
  2. var scoreQuery =
  3.           from student in students
  4.           from score in student.Scores
  5.           where score >90
  6.           select new{Last= student.LastName, score };

Security 安全性


 

Follow the guidelines in Secure Coding Guidelines.

请遵循代码安全维护指南中的准则。

posted @ 2017-04-11 09:41  只追昭熙  阅读(1646)  评论(0编辑  收藏  举报