Objective C # study Note 1

Source: Internet
Author: User

Clause 1: Use properties to replace accessible data members
1. properties in C # Seem to be a data member during access, but they are implemented in a way! You can check the parameters of the Set accessors! Multi-thread support for Attribute access!
Public class person
{
Private string name;
Public string name
{
Get
{
Lock (this)
{
Return name;
}
}
Set
{
If (value = NULL | value. Length = 0)
Throw new argumentexception ("name cannot be blank", "name ");
Name = value;
}
}
}

2... NET data binding only supports attributes and does not support public data members. The direct exposure of data members does not comply with the object-oriented design principles!

3. attributes can be defined as read-only, write-only, and read-write attributes.
Public class person // read-only!
{
Private string name;
Public string name
{
Get
{
Return name;

}
}
}

4. C #2.0 you can use the access modifier to modify get. Set accessors are essentially member functions to better control attribute visibility!
Public class person
{
Private string name;
Public string name
{
Get
{
Return name;

}
Protected set
{
Name = value;
}
}
}

5. Virtual and abstract attributes can be defined!
Public class person
{
Private string name;
Public Virtual string name // The virtual attribute can be implemented by override for Polymorphism
{
Get
{
Return name;
}
Set
{
Name = value;
}
}
}
Public interface iperson
{
String name // the implementation of the inheritance type of the abstract attribute must be provided.
{
Get;
Set;
}
}

6. Use the this keyword to define attributes with parameters!
Int [] values;
Public int this [Index]
{
Get
{
Return values [Index];
}
Set
{
Values [intex] = value;
}
}

7. The property is different from the Il generated by the public field. Customers who access attributes and common fieldsCodeGenerating Il is also different. Therefore, attributes and common fields are incompatible in binary format! You cannot simply replace common fields with attributes. In this way, all fields used must be re-compiled.ProgramSet! They onlySource codeLayers are compatible!

8. The JIT editor performs inline Optimization on short frequently called methods. (Including the property accessors). Therefore, if JIT has inline attribute accessors. There is no difference in the efficiency of attributes and fields. Even if not inline. The difference in efficiency is negligible.

To sum up, we should use attributes!

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.