. Net Project Code Style

Source: Internet
Author: User
Tags deprecated

1. C # code style requirements 1.1 Notes

Type, properties, events, methods, method parameters, and add comments as needed .

If the name of a type, property, event, method, method parameter is already self-explanatory, no comment is required, otherwise you need to add a comment.

When you add a comment, the method is added as shown:

1.2 Types (classes, structs, delegates, interfaces), fields, properties, methods, naming of events

English is preferred, if the English does not have the appropriate word description, you can use pinyin, the use of Chinese is not in line with the requirements.

The only place where you can use Chinese is an enumeration of enumerated items, which are not actually part of the title of this section. Here just put together a description, as shown in:

1.3 Do not use abbreviations

Names of all types, methods, parameters, and variables must not be abbreviated, including the familiar abbreviations, such as MSG.

1.4 Code Use semi-expansion

The first step is to open visual Studio and go to Tools, Options ..., as shown in:

In the second step, go to "text editor", "C #", "Formatting", "New Line", and uncheck the check boxes on the right, as shown in:

Third step, click "OK" to complete the setup.

1.5 use Tab as indentation and set indent size to 4

The first step is to open visual Studio and go to Tools, Options ..., as shown in:

In the second step, go to "text editor", "C #", "tab", as shown, set tab.

Third step, click "OK" to complete the setup.

1.6 A. cs source file with a maximum of two types defined

If two types of relationships are closely related, such as products, product types, the product class, and the ProductType enumeration can be defined in the same Product.cs file.

However, you cannot have two unrelated type definitions in a. cs file, such as defining the product class and the Reseller Class (reseller) in a BasicInfo.cs file.

1.7 type name and source file name must be the same

When a type is named product, its source file name can only be Product.cs.

1.8 All namespaces, type names using Pascal style (capitalize the first letter of the word)

As shown, the red flag is used in the Pascal style type:

Note that ProductType is a private type, regardless of whether the type is public or private, and its name is always in Pascal style.

1.9 Local variables, method parameter names using camel style (first letter lowercase, followed by the first letter of each word capitalized)

Red is marked with a camel-style variable or method parameter:

1.10 Private methods, protected methods, still named with Pascal style

1.11 If the IF statement has only one row, it can be without braces, but must be in the same row as the IF statement

The If judgment in example 1.9 is actually equivalent to the following statement:

1.12 Call the other members of the type, add this; Call the parent class member, add base

The sample code is as follows:

1.13 Type internal private and protected fields, named with Camel style, but prefixed with "_"

The code examples are as follows:

1.14 Public fields cannot appear

If a public field is required, the property is used for wrapping.

1.15 Sort order of type members

The sort order of type members is from top to bottom:

Fields: Private fields, protected fields

Properties: Private property, protected property, public property

Events: Private events, protected events, public events

Constructors: Constructors with the highest number of parameters, constructors with a medium number of parameters, constructors with the fewest number of parameters

Method: The overloaded method is arranged in the same order as the constructor, from the maximum number of parameters down to the lowest parameter.

1.16 Naming of delegates and events

The delegate is named EventHandler as a suffix, such as salesouteventhandler.

The event takes its corresponding delegate type, removes the EventHandler suffix, and adds an on prefix.

For example, for an event of the Salesouteventhandler delegate type, its event name is: Onsalesout.

The sample code is as follows:

1.17 Returns the method of type bool, the name of the property

If the type of the method returned is type bool, it is prefixed with is, can, or Try, for example:

1.18 Common collection type suffix naming

The corresponding suffix should be added to match the collection types listed in the following table.

1.19 common suffix naming

The corresponding suffix must be added to the local variables, method parameters, fields, and properties listed in the table below.

Description

Suffix Example Example description
Fee-related Cost Shipcost Shipping
Price related Price Productunitprice Product price
Message-related Message (Discard note) Successmessage Success Message
Date related Date (deprecated time) OrderDate Date of order
Count, quantity related Count (deprecated time) Logincount Number of Logins
Link Address related Url Blogurl Blog links
Photo related Image Signimage Signature picture
Amount related Amount Prepaidamount Advance
Points, integral related Point Memberpoint Member points
Records, log related Record (Discard log) Errorrecord Error logging
Configuration related Config Databaseconfig Database configuration
Status related Status Orderstatus Order Status
mode, method related Mode OpenMode Open mode
Kinds of related Category/type two Choose one UserCategory Type of user
Factory class related Factory ConnectionFactory Connection Factory
Enable related Enabled Exportenabled Turn on Export
Flow-related Stream Uploadstream Upload Stream
Reader-related Reader Excelreader Excel Reader
Writer-related Writer Excelwriter Excel writer
Adapter-related Adapter Introopadapter Introop Adapter
Provider-related Provider Memebershipprovider Member Information provider
Wrapper-related Wrapper Productwrapper Product Wrapper
Connection related Connection Excelconnection Excel connection
1.20 common Type naming

Where the types in the following table exist, they should be named with the name specified in the following table.

1.21 Common fields, property naming

Fields and attributes are more complex, so only the most commonly used items are listed.

Reference: http://www.cnblogs.com/JimmyZhang/archive/2013/06/05/3118936.html

. Net Project Code Style

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.