Solution to the original encapsulation problem in C #

Source: Internet
Author: User

Encapsulation attributes
The iterator automatically reads data using the get identifier.
The producer automatically writes data using the set identifier.

Sample Code:
Struct Time
{
...
Public int Hour // No (), H instead of h
{
Get {...}
Set {...}
}
Private int hour, minute, second;
}
Time lunch = new Time ();
...
Lunch. Hour = 12;
...
Console. WriteLine (lunch. Hour); www.2cto.com
C # provides a good solution to the above problems. You can combine get and set functions into a simple attribute. The attribute Declaration includes an optional access modifier (public in this example), return value (int), attribute name (Hour), and an attribute Body Containing get and set statements. Note that the attribute does not contain parentheses because the attribute is not a function. The naming rules of attributes should comply with general naming rules, that is, public PascalCase rules, rather than public camelCase rules. In the preceding example, the Hour attribute is public, so it is named Hour instead of hour.
The example demonstrates the usage of attributes. The syntax used by the attribute is the same as that of the field, with no parentheses. If you want to write an attribute, you can write it as follows:
Lunch. Hour = 12;
The set statement of the attribute is automatically executed.
If you want to read an attribute, you can write it as follows:
Int hour = lunch. Hour;
The get statement of the attribute is automatically executed.


Author: ershouyage

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.