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