C # programming style

Source: Internet
Author: User

After I started my internship, I realized how delicious I was. There are still many things to learn.

The company is very good and can help you buy books. One day, I casually asked my boss D,CodeWhat are the requirements of the specifications. Then D found the book "The Elements of C # style" on Amazon (Comparison between Chinese and English), so that I could buy it directly, write as required. You can ask Secretary F for reimbursement.

Orders were placed in Amazon last Thursday and arrived only on Monday. This book is really a little slow. It doesn't matter. I can read it quickly. From Monday to Friday, I spent every day commuting to and from the subway (Guangzhou Metro, you know). I finished reading my book within five working days. Of course, the key is thin enough. The description is also concise.

The following are some of my memories:

1. Blank

This is nothing to say. It means to leave it blank and leave it in a decisive manner. Don't be afraid of waste. Blank Space helps improve code readability.

2. Block

The Section enclosed by braces "{" and.

3. Hump method (small hump method)

Variables are generally identified by the small hump method. The camper method means that except the first word, the first letter of other words is capitalized. For example

 
IntMystudentcount;

The first word of the variable mystudentcount is in lowercase, and the first letter of the subsequent word is in uppercase.

4. Pascal method (big hump method)

Compared with the small camper method, the big camper method also upper the first letter of the first word. It is often used for class name, function name, attribute, and namespace. For example

 
Public ClassDatabaseuser;

 

5. Global Variables

Generally, a prefix or suffix is used for private and local variables. It is usually identified by an underscore. For example

 
Private IntMystudentcount _;

 

The underline is added to the front and the back. Personal habits are always at the top.

6. Constants

Constants are usually written in uppercase and separated by underscores. For example

 
Private Int ConstStudent_count =50;

 

7. Switch

When a switch is used, the default statement should be added regardless of whether case covers all cases. In many cases, the results are beyond our consideration. Cover all situations as much as possible. For example

 Switch  (Month ){  Case   1  :  //  ...          Break  ;  Case   2  :  //  ...          Break ;...  Case   12  :  //  Code...          Break  ;  Default  :  //  Code...          Break  ;} 

 

The code is used to handle different situations every month. Although it has been used for 12 months, it is recommended to add default to the code just in case.

8. Binary Operators

Generally, a space is required between the left and right sides of the binary operator. Of course, people who use vs generally know that when you press the final identifier ";" of each code sentence, Vs will automatically process the code and add a space to the left and right sides of the binary operator. For example

 
Count = count + I;

 

It can be found that there is a space around "=" and "+. This looks much better than without spaces. Don't believe it. Don't try it.

9. conditional statements

Try not to use a direct value above the add statement. For example

 
IntCount =60;If(Count _> =Count ){//Code...}

 

We recommend this approach, but we do not recommend

If(Count _> =60){//Code...}

 

This approach. When changes are to be made, the value of the former is quite obvious.

10. use aliases

Use the alias of the type name whenever possible. For example

 
IntCount =60; System. int32 count2=60;

 

In fact, int is only the alias of system. int32, and the effects of the two are the same. However, we recommend the former instead of the latter. There are historical factors, but as long as they are individuals, they will consciously write aliases such as int and double.

11. Atomic Method

We recommend atomic operations. A function does not need to do much. Especially for classes. If an implementation can be completed using several atomic methods, we recommend this approach, rather than writing the method too long. It is best not to exceed 100 rows.

12. Access Control Mark

You can leave the Access Controller unspecified by default. However, we do not recommend this approach. We recommend that you write access permissions to all variables, methods, attributes, and... in the class.

13. Region

# Region and # endregion are very useful. It can be used anywhere, bored, or in the same block at the same time, to contract the code. In a class, we recommend that you use region to classify the code. For example

 

Old people have poor memory.

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.