. NET Framework-Example of C # programming style code given by Microsoft

Source: Internet
Author: User


From MS official guideline:

1 We Use the Allman style braces, where each brace begins to a new line.

while (x = = y) {    something ();    SomethingElse ();} Finalthing ();

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

3 We Use the CamelCase for internal and private fields and the use readonly where possible. Prefix instance fields with, the static fields with S_ and the thread static fields with T_. When used in 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 is specified at the top of the file, outside of Namespace declarations and should be sorted ALP Habetically.

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, does not has a lines between members of a type.

8 Avoid spurious free spaces.

if (Somevar = = 0) ...,

9 If A file happens to differ on style from these guidelines (e.g. private members is named M_member rather than _member) The existing style in that file takes precedence.

We only use Var when it's obvious what's the variable type is.

var stream = new FileStream (...)//var is Okayvar stream = Openstandardinput ()//This is not good

We use language keywords instead of BCL types.

int, string, float//Good Int32, String, single/Bad

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

Private Const int age=100; Good

We use nameof(...) instead of "..." whenever possible and relevant.

Should is specified at the top within type declarations.

When including non-ascii characters in the source code use Unicode escape sequences (\uxxxx) instead of literal char Acters.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.