C # project code specification,

Source: Internet
Author: User

C # project code specification,

C # project code specification

Preface

A small dish is a small dish. A few people developed a project, let the company's Daniel read it a little, and finally sent out four terrible words. It is named in a variety of ways, including uppercase letters, lowercase English letters, uppercase pinyin, lowercase pinyin, English and Pinyin combinations, letters and special characters (underlines, etc. If such project code is maintained or added to a function, it will be a headache to search for bugs. No one can tell us that we are little dishes? What's the biggest headache for programmers: http://kb.cnblogs.com/page/19#/in this knowledge blog, the final result is a name. So ......

Then, based on the existing projects, I checked various blog articles in the blog garden and summarized them. Of course, this certainly has many advantages.

1. Facilitate Code Communication and maintenance.

2. It does not affect the coding efficiency and does not conflict with public habits.

3. Make the code more beautiful and easier to read.

4. Make the code logic clearer and easier to understand.

The two encoding methods in C # are as follows:

Camel (Camel): case-except the first word, the first letter of all words is in upper case, and the other letters are in lower case.
Pascal (Pascal): case-uppercase and lowercase letters are the first and other letters of all words.

The C # code specification in this article mainly refers to the great god's specification: Coding.

C # code specification

1. Type (class, structure, Delegate, interface), field, attribute, method, and event naming

English is preferred. If there is no proper English description, you can use Pinyin.

 

2. Do not use abbreviations

  Do not use abbreviations for all types, fields, attributes, methods, and events, including well-known abbreviations, such as msg.

 

3. Do not use a single letter variable

Do not use variables of a single letter, such as I, m, n, or index, except for those used for loop iteration.

 

 

4. Use Tab as the indent and set the indent size to 4.

 

5. Notes

Type, attribute, event, method, and method parameters. Add comments as needed.

If the name of the type, attribute, event, method, and method parameter is self-explanatory, no comment is required;

Otherwise, add a comment.

 

6. The type name is the same as the source file name.

When the type is Product, the source file name can only be Product. cs.

 

7. All namespaces and type names use the Pascal Style

 

8. Use the Camel style for local variables and method parameter names (no underline is used)

Variables or method parameters marked in red using the Camel Style

 

9. In a class, each method requires an empty row (preferably an empty row)

 

10. Avoid using large files. If the code in a file exceeds-lines, you must consider separating the code into different classes. At the same time, avoid writing too long methods. If the code of a method is too long (the number of rows of the method is not explicitly indicated at the moment), you should consider breaking it into different methods.

 

11. One method only completes one task. Do not combine multiple tasks into one method, even if those tasks are very small.

 

12. For other members of the call type, this must be added. For the parent class member to be called, base must be added.

 

13. Do not use the specific path and drive name in the code. Use relative paths and make paths reusable

 

14. Do not "catch exceptions but do nothing". If an exception is hidden, you will never know whether the exception has occurred.

 

15. if the content of the if statement block has only one row, no curly braces can be added, and it is better to be in the same line as the if statement.

 

16. Private and protected fields in the type are named in the Camel style, but prefixed "_".

 

 

17. sorting order of type members

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

Field: Private field, protected field

Attributes: private, protected, and public attributes

Events: private events, protected events, and public events

Constructor: constructor with the largest number of parameters. constructor with the lowest number of parameters

Method: The sorting order of the overload method is the same as that of the constructor. the maximum number of parameters is down to the minimum number of parameters.

 

18. Delegate and event naming

The delegate is named after EventHandler, for example, SalesOutEventHandler.

The event is composed of the EventHandler suffix and the On prefix.

The sample code is as follows:

 

19. Return the names of methods and attributes of the bool type.

If the type returned by a method Is bool, its prefix Is, for example, IsHidden.

If the type of an attribute is bool, its prefix is Can, for example, CanHidden.

 

20. Names of Common Set suffix types

If the set types listed in the following table are met, the corresponding suffixes should be added.

 

21. Names of common fields and attributes

Fields and attribute types are complex, so only the most common items are listed.

 

Summary

The specifications in this article will be used for reference in the next new project. problems or comments encountered during use will be reported to this Article. We also invite you to refer to them, optimization.

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.