How to Use get and set statements in C #

Source: Internet
Author: User

How to use the set statement
• Set statement
Values are assigned by value identifiers.
Statement can contain any statement (or even no statement)
Struct Time
{
...
Public int Hour
{
...
Set {
If (value <0 | value> 24)
Throw new ArgumentException ("value ");
Hour = value;
}
}
Private int hour, minute, second;
}
Time lunch = new Time ();
...
Lunch. Hour = 12;
When writing an attribute, the set statement of the attribute runs automatically.
In the preceding example, the Time structure class has an integer property Hour, so the value assigned to this property must be an integer value. For example:
Lunch. Hour = 12;
Assign an integer value 12 to the Hour attribute of lunch. This statement automatically calls the set statement of the attribute. The set statement obtains the attribute value assignment through the value identifier. For example, if 12 is assigned to the Hour attribute, the value of vaue is 12. Note that value is not a keyword. Value is only an identifier in the set statement. You can declare value as the name of a variable in any statement other than the set statement. For example:
Public int Hour
{
Get {int value;...} // correct
Set {int value;...} // Error
}


How to Use get statements
Except get statement
Struct must return a value of a definite type.
The get function is similar to the get function"
Struct Time
{
...
Public int Hour
{
Get
{
Return hour;
}
...
}
Private int hour, minute, second;
}
Time lunch = new Time ();
... Console. WriteLine (lunch. Hour );
// Please note that get and set are not keywords www.2cto.com
When reading an attribute, the get statement of the attribute runs automatically.
The get statement must return a value of a specific type. In the preceding example, the Time structure class has an integer property Hour, so its get statement must return an integer value.
The Return Value of the attribute cannot be void (from here we can infer that the field type cannot be void ). This means that the get statement must contain a complete return statement (retun; this form is incorrect ).
A get statement can contain any other statement before a retun statement (for example, you can check the type of the variable), but a return statement cannot be omitted.
Note that get and set are not keywords, so you can declare a local variable in a get/set statement anywhere. The constant name is get or set, but it is best not to do so.


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.