.NET架構-微軟給出的C#編程風格代碼執行個體

來源:互聯網
上載者:User


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 OKayvar 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.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.