Objective C # Reading Notes (1): C # Language Elements

Source: Internet
Author: User

1. Use properties to replace accessible data members

As long as we want to expose data to public or protected interfaces of the type, we should use attributes for implementation. Indexes should be used for types with sequence or dictionary features. All data members should be declared as private. The benefits of using attributes are obvious: we can get better support for data binding, and we can easily make any changes to the implementation of its access methods in the future. Encapsulate variables in attributes with only a few lines of code added. If you first use a data member and later find that you need to use the attribute, then the cost of modifying it will be several hours. A little investment today will save a lot of time for tomorrow.

The JIT compiler performs inline processing on some method calls (including attribute accessors). The efficiency of attributes and data members is no different. The timely attribute accessors are not inline. The actual efficiency difference is negligible compared with the cost of function calling. This difference is worth noting only in rare cases.

 

2. readonly is better than Const)

In C #, we use the readonly keyword to declare the runtime constant, and the const keyword to declare the compilation constant.

The compilation constant is replaced with the value of this constant in the compiled result code. The runtime constant is calculated at runtime. After compilation, the code will maintain reference to the readonly variable (rather than its value.

The compile-time quantity can only be used for value types, while the run-time quantity is unlimited.

The only advantage of using const over using readonly is performance: using a known constant is more efficient than accessing the readonly value, but the efficiency is very small.

Const should be considered only when the value of the variable must be available during compilation in some cases. For example, parameter of Attribute Class, enumeration definition, and some values that do not change with the component version. Otherwise, the readonly constant should be selected first in any other case to obtain its flexibility.

 

3. The operator is or as is better than forced Transformation

Good object-oriented practices tell us to avoid transformation, but sometimes we have no choice. We should try to use the AS and is operators to clearly express our intent during the transformation.

We should use the as operator when we want to perform transformation immediately. If you only need to judge the type and do not need to perform transformation immediately, use is.

 

4. Use the conditional feature instead # If condition Compilation

Using the conditional feature is more effective than using the Il code generated by # If/# endif. At the same time, we can clearly separate the conditional line code at the function level, so that our code has a better mechanism. In addition, the C # compiler also provides good support for this, which helps us avoid errors that were previously made when # If or # endif was used.

 

5. The tostring () method is always provided.

The default tostring () method provided by system. Object returns the type name. We can simply rewrite the tostring () method to meet our requirements. You can also use the reload tostring () method to specify your own format for our type to meet more complex requirements.

 

6. Use foreach loop statements first

Foreach is a very useful statement. It uses the most efficient structure to generate correct code for "Upper and Lower Bounds indexes of arrays", "multi-dimensional array traversal", and "operand transformation, in addition, the most efficient cycle structure is generated. It is the best way to traverse a set.

Foreach can be used for any collection type. There are three ways to make a class support the foreach method:

(1) The type has a public getenumerator () method;

(2) Type display implements the ienumerable interface;

(3) type implements the ienumerator interface.

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.