C # code specification (unfinished version)

Source: Internet
Author: User

First, the file name

1 File name

The file name uniformly uses the Pascal nomenclature, named after the C # class name, expands the lowercase name.

Example:

GameManager.cs

  2 File comments

Each file header must contain a comment description.

Example:

// file name: GameStatusMachine.cs
//// file Function Description://// Game State Machine base class script for reading and writing global game state, sending game state to control game scene switching, etc.//// Create identity: Taixihuase 20150327//// Modified ID: taixihuase 20150328// Modify Description:// Add the enumeration value LoginStatus in the game state enumeration GameStatus, indicating the login status//// To Modify the identity:// Modify Description://

File header comments follow these rules.

Rule 1: The file feature in the file header comment is just a brief description.

Rule 2: Create an identity or modify the identity consists of the creator or the modified person and the date.

Rule 3: The same person on the same day multiple changes to the file only need to make a change in the identity of different people in the same days to modify the file will need to make another change in the identity.

Rule 4: Note unify the double left diagonal bar, do not use C-style annotations, avoid clutter annotations such as planetary fill vacancies, keep annotations tidy

Rule 5: When necessary, use a blank comment between multiple comment content segments instead of using a blank line

Second, the Code style

1 Column width

When you write code, switch to the screen high resolution, for example in Visual Studio 2013 at 1440*900 resolution, where code is controlled within 110 columns when code is scaled to 100%.

  2 line break

When an expression exceeds or is about to exceed the column width, the following line break rules are followed.

Rule 1: Wrap the line before the operator.

Rule 2: line breaks after commas, precedence over rule 1.

Rule 3: This rule has the highest precedence, using the IDE's own wrapping, rather than manual line wrapping.

 

  3 Indent

Each line starts with a tab, which takes up 4 characters in length. Instead of typing 4 spaces directly, use the TAB key. Instead of using tab tabs, you should set the indent size to 4 in the IDE, and convert tab. In Visual Studio 2013, for example, here's how:

Tools, Options, text Editor->c#-> tab, insert spaces

  4 Blank Line

Use empty lines to block logically related code to improve the readability of your code.

The use of blank lines follows the following rules.

Rule 1: Use two blank lines between classes above the class level.

1. Interfaces and interfaces, classes and classes, interfaces and classes are defined.

2. The enumeration is between the interface, the enumeration, and the definition of the class.

3. Between namespaces and namespaces.

Rule 2: Use a blank line between the levels in the class.

1. Variables and variables, methods and methods, properties and properties, methods and properties, methods and fields.

2. Between the variable declaration and the statement.

3. Between different logical blocks in the method.

4. The method returns between the statement and the other statements.

5. The attribute is between fields that do not correspond to this property.

6. Comments are related to other pieces of code that consist of multiple non-comment statements that work together to handle a certain logic.

Rule 3: Do not use blank lines between tightly associative content within a class.

1. Between comments and other comments.

2. The property is between the fields associated with the property.

3. Note The fields, properties, methods, interfaces, classes, and namespaces that correspond to the comment.

Example:

// This is a C # code specification namespacenamespacecsharpstyleguide{// This is an interface    Interfaceinothing {voidNothing (); }//Interface inothing
// This is the first example class classFirstsampleclass {Private Static stringClassName ="Firstsampleclass"; Private intProperty//This is aprivate field, where one of the two scenarios permitted by the end of line comment is used, and the other is associated with a closing parenthesis Public intProperty {Get{returnProperty ;} Set{property =value;} } Public intAnotherproperty {Get;Set; } // default constructor// with a default parameter of value, the default value is 0Firstsampleclass (intValue =0) { property=value; } Public intGetValue () {// Display the prompt informationConsole.WriteLine ("The class name of this class is"+className); Console.WriteLine ("The value size stored by this class is:"); // single-line display valuesConsole.WriteLine (property); returnProperty ; } Public Virtual voidSetValue (intvalue) { This. property =value; }}//Class Firstsampleclass// set the Main method entry within this class classProgram {Static voidMain (string[] args) { // don't do anything for the moment.}}//Class program}//Namespace Csharpstyleguide

  5 spaces

Priority is to use the IDE's own indentation to fill in spaces, and spaces follow these rules.

Rule 1: The keyword and the opening parenthesis are separated by a space.

Example:

     while (true)

Rule 2: No spaces are used between the method name and the opening parenthesis.

Example:

    Showsample ();

Rule 3: Multiple parameters are separated by commas, and a space is added after each comma.

Example:

    CompareValue (LHS, RHS);

Rule 4: Except. And, the two-dollar operator is separated from its operand by a space, and the unary operator is not separated by a space.

Example:

    A + = b    ; = A/(A + b);    a+ +;

Rule 5: Expressions in statements are separated by spaces.

Example:

     for (int0; ++i)

Rule 6: Note the left diagonal bar and the contents of the comment are separated by a space

Example:

    // There's a space in front of this comment .

  6 Brackets

Parentheses: Use parentheses first to follow the rules that use spaces, and second, do not use parentheses in the return statement if it is not necessary.

Example:

    return ERROR;

Curly braces: Follow the requirements below when using.

Rule 1: The left curly brace is placed and aligned with the next line of the keyword or method name.

Rule 2: The left curly brace is aligned with the right curly brace.

Rule 3: Curly braces line up alone, not with any statement.

You must use curly braces after the rules 4:if, else, while, and do, even if the interior is empty or there is only one statement, which makes it easy to extend content and fast search.

Rule 5: When a block of code is longer (nearly or beyond a screen), it is recommended to add a comment after the right curly brace to find the corresponding entry condition, and a space between the comment and the closing curly brace

Example:

     while (true)    {        if  (isOk)        {            return  OK;        }         Else         {            // do somthing here.            return ERROR;         // IsOK is False    // true

C # code specification (unfinished version)

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.