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


本文摘自PHP中文网,作者黄舟,侵删。



from ms official guideline:

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

1

2

3

4

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.

1

private string _foo //betterstring _foo  //bad

1

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.

1

2

3

4

5

6

7

8

9

10

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.

1

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.

1

2

var stream = new FileStream(...) //var is OKay

var stream = OpenStandardInput() // here var is not good

11 We use language keywords instead of BCL types.

1

2

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.

1

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#编程风格代码实例的详细内容!

相关阅读 >>

asp.net在网站根目录下创建文件夹

c#中的数据类型是什么?c#中的四种数据类型解释

c#正则表达式元字符详解

c#实现网络电子白板、课件功能 (在线教学系统)

c#学习记录:编写高质量代码改善整理建议1-3

c#接口(interface)

具体介绍c#使用selenium+phantomjs抓取数据的案例(图文)

使用c#操作windowad之添加对象到用户组

c# 实现 cachehelper

c#中的console.read()方法详解

更多相关阅读请进入《.NET框架》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...