Read Summary (8) attributes

Source: Internet
Author: User

  The attribute looks like a field, but it is essentially a method. The attribute is used to maintain a good data encapsulation, so that data fields of the type are not exposed, so that the object state is never damaged.

1. Define attributes

The Code is as follows:

Public sealed class employee {// Private field (supported field) Private string name; private int age; Public string name {Get // get accessor {return name ;} set // set accessors {name = value; // value is a hidden field, always representing a new value} public int age {get {return age ;} set {// you can add the judgment logic if (value <0 | value> 100) {Throw new argumentoutofrangeexception ("value", value. tostring (), "the value must be between 0 and 100! ") ;}Age = value ;}}}
Ii. Call Properties

The Code is as follows:

Class program {static void main (string [] ARGs) {employee e = new employee (); E. name = "McGrady"; // set employee name string employeename = E. name; // get employee name console. writeline (employeename); // display "McGrady" E. age = 32; // set employee age E. age =-5; // throw argumentoutofrangeexception int employeeage = E. age; // get employee age console. writeline (employeeage); // display "32 "}}

Conclusion: 1. You can think of attributes as intelligent fields, that is, fields with additional logic.
2. Each attribute has a name and a type, and the type cannot be void, and the attribute cannot be reloaded.
3. Read-only or write-only attributes can be defined. The set method is omitted to define a read-only attribute, while the get method is omitted to define a write-only attribute.
4. the compiler automatically generates the names of these methods by appending the GET _ or set _ prefix before the attribute name you specify.
5. In addition to generating accessors, the compiler generates an attribute definition item in the metadata of the hosted assembly (DLL) for each attribute defined in the source code.

Iii. Automatic attributes of AIP

Automatic attribute AIP (automatically implemented property) is a more concise way to define attributes. For example, the attributes in the preceding example can be defined:

Public sealed class employee {// automatic attribute public string name {Get; set;} public int age {Get; Set ;}}

Of course, its calling method and running result are no different from common attributes. Note that:

1. You cannot add breakpoints to the get and set methods of AIP, which is not conducive to debugging and error detection.
2. the AIP attribute must be both readable and writable. That is to say, the compiler must generate both get and set methods.

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.