C # tips (1) -- Book notes for "Better CSharp code,

Source: Internet
Author: User

C # tips (1) -- Book notes for "Better CSharp code,

Using commands similar to the Hungarian naming method as controls, other variables are not required
E.g. Button btnAdd;
E.g. TextBox txtUsername;
E.g. ListBox lstFileNames;
E.g. Panel panelInfo;

Using the Camel rule for naming function parameters, local variables, private and protected fields (lowercase letters ), in other cases, such as namespace, structure, class, interface, enumeration, enumeration items, attributes, methods, events, delegate, and public fields, Pascal rules are used (uppercase)

Annotations. It uses XML document comments and works well with the code intelligent Awareness System in VS development environment.

Use '@' to avoid code switching.
E.g. string Path = @ 'd :\';

Use string. Empty to represent an Empty string, instead of using two connected quotation marks to represent an Empty string.
E.g. if (path = string. Empty)

Use the string. IsNullOrEmpty method to test null or empty strings.
E.g. if (! String. IsNullOrEmpty (str )){...}

Pipeline and StringBuilder can greatly improve the processing performance of strings. When you need to perform frequent operations on strings, StringBuilder is much more efficient than String.

Initialize, Initialize all variables, You can initialize the reference type to null, the value type to 0, etc.
E.g. private Image image = null;

When to use enumeration?
· Use enumeration instead of Boolean unless you are sure that there will never be more than two types of values.
· The code that uses enumeration parameters is more descriptive than the bool type.
· If we care about numeric values, we should use named constants instead of enumeration.
E.g. public const int DaysInSmallMonth = 30;
Public const int DaysInBigMonth = 31;

Ghost, do not use magic number (magic number)

=========================================>
Not yet complete, constantly updated during the course of reading


Symbol in C Language <Yes

Left shift operator (<)

Removes all the binary bits of an operation object from the left and adds 0 to the right ).

For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,

Move 1 to the left and then a = a * 2;

If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)

Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.

The operand shifts one digit to the right, which is equivalent to dividing the number by 2.

For example, a = a> 2 shifts the binary bit of a two places to the right,

0 or 1 to see whether the number is positive or negative.

Symbol in C Language <Yes

Left shift operator (<)

Removes all the binary bits of an operation object from the left and adds 0 to the right ).

For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,

Move 1 to the left and then a = a * 2;

If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)

Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.

The operand shifts one digit to the right, which is equivalent to dividing the number by 2.

For example, a = a> 2 shifts the binary bit of a two places to the right,

0 or 1 to see whether the number is positive or negative.

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.