Objective C # Reading Notes (1)

Source: Internet
Author: User
Mr. Bill wner's book "Objective C #" maintains the author's consistent style and is worth reading. I have long wanted to write down my thoughts on reading this book, but I have never had time. Recently, I have been idle. I hope to help you with my experience in writing this book.

Item 1: Always Use Properties Instead of Accessible Data Members
Item 1: Always use attributes instead of directly accessing data members

When it comes to attributes, everyone must know.
For example:

Class Person
{
Private string name;
Public string Name
{
Get {return this. name ;}
Set {this. name = value ;}
}
}

What are the benefits of specific attributes? Why must I use attributes instead of public? These must be what everyone wants to know most.

In this Item, we introduce the following benefits of using attributes. 
1. Verify the value assignment or perform other operations.
2. Follow the encapsulation principles.
3. Convenient synchronous thread access.
4. Supports virtual and abstract attributes and can be used in interfaces.
5. in C #2.0, the get and set methods of attributes support different access permissions.

So why do we need to make the code complex to use attributes without directly using public for access? Isn't that much simpler? Many people have such doubts. Let's come
See what is the difference between attribute access and public access.

First, let's take a look at using public access. When we create a class instance, dynamic memory will be allocated for the public field.

When defining an attribute through attribute access, the compiler will generate the following three items in the generated managed module:
· One get accesser method that indicates the attribute. Only the get accesser method is defined.
· One set accessors method that represents the attribute. Only the set accessors method is defined.
· 1 Property definition in the metadata of the hosting module.

When we use attributes, Get and Set are marked as hidebysig and specialname. That is to say, they cannot be directly called by C # source code, and they are not exactly the class.
Type Definition. You can only access them through Property. The JIT compiler will perform inline processing on the Code accessed by the attribute, so that the runtime will not be available when the attribute is used.
Loss (relative to field access ). However, it should be noted that attributes should be used only for operations with short execution time, and for operations with long execution time
Method.

PS: inline refers to directly compiling the code in one method into the method that calls them, so as to eliminate the running burden when calling the method, but its cost is the compiled method code.
It becomes relatively large. Attribute accessors usually contain less code, so inline operations will make the code smaller and run more efficiently!

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.