首页 > 后端开发 > C#.Net教程 > .NET框架-微软给出的C#编程风格代码实例

.NET框架-微软给出的C#编程风格代码实例

黄舟
发布: 2017-03-18 11:39:02
原创
2036 人浏览过



from ms official guideline:

1 We use Allman style braces, where each brace begins on a new line.

while (x == y){
    something();
    somethingelse();
}finalthing();
登录后复制

2 We use four spaces of indentation (no tabs).

3 We use camelCase for internal and private fields and use readonly where possible. Prefix instance fields with , static fields with s_ and thread static fields with t_. When used on static fields, readonly should come after static (i.e. static readonly not readonly static).

4 We avoid this. unless absolutely necessary.

5 We always specify the visibility, even if it’s the default. Visibility should be the first modifier.

private string _foo //betterstring _foo  //bad
登录后复制
public abstract //betterabstract public //bad
登录后复制

6 Namespace imports should be specified at the top of the file, outside of namespace declarations and should be sorted alphabetically.

using System.IO;using System.Collections;

namespace CAXA.MES.UI.Performance.Board
{  public class LinkedList
  {


  }

}
登录后复制

7 Avoid more than one empty line at any time. For example, do not have two blank lines between members of a type.

8 Avoid spurious free spaces.

if (someVar == 0)...,
登录后复制

9 If a file happens to differ in style from these guidelines (e.g. private members are named m_member rather than _member), the existing style in that file takes precedence.

10 We only use var when it’s obvious what the variable type is.

var stream = new FileStream(...) //var is OKay
var stream = OpenStandardInput() // here var is not good
登录后复制

11 We use language keywords instead of BCL types.

 int, string, float // good
 Int32, String, Single // bad
登录后复制

12 We use PascalCasing to name all our constant local variables and fields. The only exception is for interop code where the constant value should exactly match the name and value of the code you are calling via interop.

private const int Age=100; //good
登录后复制

13 We use nameof(…) instead of “…” whenever possible and relevant.

14 Fields should be specified at the top within type declarations.

15 When including non-ASCII characters in the source code use Unicode escape sequences (\uXXXX) instead of literal characters.

以上是.NET框架-微软给出的C#编程风格代码实例的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板