[Advanced practice] -- Improving C # program quality (1)

Source: Internet
Author: User

This is an outline, so that you can spend less time reading it over and over again. During development, pay more attention to these useful suggestions to make yourself a better programmer. The content mainly comes from "Writing high-quality code-157 suggestions for improving C # programs" (LU Minji). This book is really good. It is a summary of some practical experience. We suggest you buy it, the suggestions here are not only applicable to C #, as long as you do. NET development, read this book will benefit from this. At the same time, I will keep updating some good programming suggestions for other books and materials.

1. Use the string.

Avoid packing; avoid allocating additional memory; Use stringbuilder instead of string. Stringbuilder is used internally in string. format, which is relatively efficient. 2. type conversion.

The conversion between primitive types (int32, double, String, object, etc.) includes implicit conversion and display conversion. The custom types can be implicitly converted using heavy-load operators (implicit ); the type provides methods such as prase and tryprase, or todouble and todatetime. The help class is system. CONVERT Converts primitive types. bitconverter converts primitive types and arrays. Display and implicit conversion between parent and child classes. 3. As and is.

Try not to use the force conversion method after the is judgment, and use the as operator directly to reduce a transformation. 4. tryparse is more efficient than parse.

Tryparse does not throw an exception. If parse is used in a loop, frequent exceptions will greatly damage the performance. 5. Use Int? Make sure that the value type can be null.

The value type can be implicitly converted to a null value type. Can the hasvalue method be used to test whether the null type contains values ?? Operator simplified code. 6. readonly and Const.

Const is a constant during the compilation period, and readonly is a runtime constant. Const can only modify the primitive type, enumeration type, and string type. readonly can modify any type. Const is inherently static and does not need to be added with a static modifier. Readonly is more flexible than const, but has a slight performance loss. We recommend that you use readonly. 7. Use 0 as the default value of Enumeration type.

When the enumeration type is declared, the default value is 0. If the 0 value does not correspond to the corresponding enumeration value, the service is meaningless. 8. Do not provide displayed values to enumeration elements.

To add an element in the middle of the enumeration, You have to modify the values of all subsequent elements. In this case, one exception is that the flagattribute enumeration is applied and the value assignment can be displayed. 9. Used to overload operators.

If you have designed a class that has more meanings, you should use the operator keyword to overload the + operator, so that users can use it more naturally. 10. When creating an object, consider whether to implement a comparator.

If the object is to be used for sorting, The icomparable interface should be implemented. If you want to change the default sorting policy, you can also customize the class to implement the icompare interface. The latter does not need to change the structure of the type, but adds a new class for implementation, which is more flexible. 11. treat differently = and equals.

Both operators can be overloaded, but both operators must ensure the semantics that true should be returned for values of the same type. For reference types, if the type points to the same object, true is returned. If necessary, rewrite the equals method to ensure that the value is equal and return true. But generally, it does not reload =, so that it retains the judgment that references equality. In addition, to determine whether two objects are the same object, you can use the object. referenceequals method. 12. Override gethashcode when rewriting equals.

This is because some Sets Based on hash values, such as hashtable and dictionary, use the hashcode value of the key to find the value. Since the meaning of equals has been rewritten, The hashcode should also be consistent with it. 13. formatted string for Type output.

One is to let the type inherit the iformatable interface, and the other is the custom formatter, which needs to implement the iformatprovider and icustomformatter. The implementation of the latter is more flexible. 14. Shallow copy and deep copy.

Both copies must be implemented by inheriting the icloneable interface. The this. memberwiseclone method can be used for the shortest copy. Deep copy can use binaryformatter to serialize an object to memorystream and then deserialize it into an object. If you want to implement both shallow copy and deep copy in a class, you can add the deepclone and shadowclone methods to distinguish them. 15. Use Dynamic to simplify reflection calls.

Dynamic is. net 4.0 introduces a new feature. The Compiler does not check dynamic variables. We can call methods on dynamic variables to check the attribute values without having to write tedious reflection code. The VaR keyword is just a syntactic sugar. After compilation, the compiler replaces the variable type with the specific variable type. After the dynamic is compiled, it is converted to the object type, calls to the other method and attributes, such as C #, are actually reflected internally, but this greatly reduces the workload of programmers.

Use Dynamic to simplify reflection calls. Dynamic is. net 4.0 introduces a new feature. The Compiler does not check dynamic variables. We can call methods on dynamic variables to check the attribute values without having to write tedious reflection code. The VaR keyword is just a syntactic sugar. After compilation, the compiler replaces the variable type with the specific variable type. After the dynamic is compiled, it is converted to the object type, calls to the other method and attributes, such as C #, are actually reflected internally, but this greatly reduces the workload of programmers.

[Advanced practice] -- Improving C # program quality (1)

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.