How much do you remember in C # programming style?

Source: Internet
Author: User

How much do you remember in C # programming style?

After starting an internship, only to find out how much food. There are still many things to learn.

The company is very good, can also help you to buy books. One day casually asked the boss D, code specifications above what requirements. Then D found the book "C # Programming style (The Elements of C # Style)" On Amazon, let me just buy it and read it and write it as requested. The book can be reimbursed to Secretary F.

Last Thursday, we took orders from Amazon and arrived in Monday. This book does come a little slow, it's OK, I see fast. From Monday to Friday, with every day commuting to the subway (Guangzhou subway you understand) time, I put the book in five working days to see the finished. Of course, the key is thin enough. The description is also quite concise.

Here are some of the parts I remember reading:

1. Blank

This has nothing to say, meaning is to leave the blank when the decisive stay, not afraid of waste. Whitespace helps improve the readability of your code.

2. Block

Generally represents the section enclosed in curly braces "{" and "}".

3. Hump method (Small Hump method)

The variable is usually identified by the small hump method. The Hump method means that in addition to the first word, the first letter of the word is capitalized. Such as

int Mystudentcount;

Variable Mystudentcount The first word is all lowercase and the first letter of the following word is capitalized.

4.Pascal method (Large Hump method)

Compared to the small hump method, the big hump method capitalized the first letter of the initial word. Commonly used for class names, function names, properties, namespaces. Such as

 Public class Databaseuser;

5. Global Variables

The general Convention is to add a prefix or suffix to private variables and local variables. Usually marked with an underscore "_". Such as

Private int Mystudentcount_;

The underline is added to the front, there are also added in the back. Personal comparative habits are added to the front.

6. Constants

Constants are usually marked with all uppercase and underlined to divide the word. Such as

Private int Const student_count=;

7.switch

With switch, the default statement should be added regardless of whether the case covers all cases. Many times the result is no longer in our consideration. All situations should be covered as much as possible. Such as

Switch (month) {Case    1:        //... break;     Case 2:        //... break;    ...     Case :        //code ... break;    default:        //code ... break;}

The code is used to handle the different situations of each month, although it has been handled for 12 months, but the code is added with the default for just in case, which is recommended.

8. Two-dollar operator

Generally, you need to add a space around the two-dollar operator. Of course, with vs people generally know when you hit the last sign of each code ";" , vs automatically processes the code, adding a space to the left and right of the two-dollar operator. Such as

Count = count + i;

You can see that there is a space around "=" and "+". This is a lot nicer than no space. Don't believe it, don't try it.

9. Conditional statements

Try not to use a direct value above the Add statement. Such as

int ; if (Count_ >= count) {    //code ...}

We recommend this approach and we do not recommend

if ) {    //code ...}

This approach. When it comes to change, the value of the former is quite obvious.

10. Using aliases

Use the alias of the type name as much as possible. For example

int  - ;

In fact, int is only the alias of System.Int32, the effect of the two is identical. However, we recommend using the former and not recommending the latter. This has historical factors, but as long as the individual, will be very consciously write int,double these aliases it.

11. Atomic Methods

We recommend atomic operations. A function does not need to do too much things. Especially for classes. If an implementation can be done in a few atomic ways, we recommend this approach, rather than writing the method too long. It is best not to exceed 100 lines.

12. Access Control Flags

Although you can default to not fill in the access control character. But we do not recommend this approach. We recommend putting all the variables, methods, properties, ... All write access rights.

13.region

#region和 #endregion is a very useful thing. It can be used in any location, whether boredom is in the same block at the same time, and the code shrinks. Within a class, we recommend using region to categorize the code. Such as

When people are old, memory is bad.

How much do you remember in C # programming 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.