C # code specification

Source: Internet
Author: User

C # code specification

  1. Naming conventions

    Object-oriented languages like C # allow developers to name classes, methods, fields, and more flexibly in a descriptive way. For these types of things, if you can more clearly describe the meaning of things, then don't be afraid to use longer names. You should use complete words instead of acronyms.

    . NET has two main types of letter-case forms: Pascal and Camel. For Pascal letter capitalization, the first letter of the first word of the created name is capitalized, as is the case with subsequent words in that name, such as Thisispascalcase. In the case of camel letters, the first letter of the first word of the created name is lowercase, and subsequent words in that name use uppercase letters, such as Thisiscamelcase.

    Hungarian notation (Hungarian notation) is a type description method used inside a name that provides a hint to the type of the named thing. We are in our own. NET coding specification, this method is used relatively conservatively. The application of this method will be found in the naming of classes that extend the exception base class (for example, illegalargumentexception) and the prefixes of GUI components (for example, Sumbitbottom).

  2. 2

    Indent in

    We can configure visual Studio to indent with tabs or spaces, as well as to set the character units for indentation. The Daily useful specification uses 4 spaces for indent units.

    The code's president is also important for readability. Try to control the code for each line within 80 characters. When there are no more than 80 characters per line, and you need to change lines, follow these rules:

    • Disconnect before operator

    • Break after a comma

    • Align Brackets

  3. 3

    declaration

        When declaring a class field, instance field, or local variable, only one is declared per line. When you make several declarations together, you need to align the names of the fields or variables. For a local variable, it should be initialized when declaring a variable, unless you are initializing the variable with some other action, such as a calculation. The

        declaration should be at the top of the class or method being declared, which makes it easier to see the claims in the future. An exception to the rule is the declaration and initialization of local variables within a for loop.

  4. 4

    Statement

    There are several types of statements in C #. Each line of code should contain no more than one statement.

    For if, If-else, and if else-if else statements, always use curly braces, and always use curly braces for the for, foreach, and while, and Do-while statements (both parts of the braces should be exclusive).

    In a return statement, you typically do not use parentheses unless you want the return value to be more pronounced.

    Here is the format for using the switch statement:

    switch (expression)

    {

    Case Constant-expression:

    statements;

    Break

    Default

    statements;

    Break

    }

    The try-catch-finally statement uses the following format:

    Try

    {

    statements;

    }

    catch (Exception)

    {

    statements;

    }

    Finally

    {

    statements;

    }

  5. 5

    Comments

    There are several types of annotations that you can use in C #. The following table shows an example of each of these types, stating the timing of each type of application.

  6. 6

    Blank lines and spaces

    Although the compiler can ignore blank lines and spaces, empty lines and spaces can separate code units of different logic, thus improving the readability of the code.

    You should use a blank line in the following situations:

    • Between methods

    • Between declarations and statements

    • Between the logical segments of the code

    • Before single-line or multiline comments

    a space should be used in the following cases:

    • After the parenthesized keyword

    • After a comma in the parameter list

    • Front and back of data operators

    • Used to separate the 3 logical segments of a statement in a for statement

  7. 7

    Solutions and Project organization

    Each solution should have the following items:

    • Business projects are only for business projects.

    • Data project handles access to databases and other legacy systems for retrieving or updating data purposes.

    • Indicates that the project (Presentiation project) handles all the views required by the end user who uses the system.

    • The test project, which holds all the unit tests for the application.

C # code specification

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.