. NET attributes and. NET attributes

Source: Internet
Author: User

. NET attributes and. NET attributes

1. What?What is property?

Attribute is the encapsulation of fields. When a field exists in the class, is read-only to control the external performance of this field (for example, accessibility? Write only? Or perform some necessary verification on the Self-read value.) We private this field and set aside a public method for accessing this field. This method is an attribute. 2. Usage? How to Use  For attributes,. NET provides two methods to define attributes. 1) general definition method// Define a private field. // 1. Ctrl + R, Ctrl + E on the field to be encapsulated. Attributes can be automatically encapsulated. // 2. You can set additional access modifiers for get and set attributes. Private string _ name; public string Name {get {return this. _ name;} set {this. _ name = Value ;}} 2. Simplified writing/* 1. This method is essentially the same as the above method, which can be seen through Reflector. The compiler automatically generates a private field for us during compilation. 2. Because private fields are added by the compiler, the name looks weird to ensure that the names are unique. In binary serialization, this write method is a little troublesome. Let's talk about it later. */Public string Name {get; set ;} 3. Essence of the Essence attributeWhat is the nature of an attribute? Have you been asked to answer this question? For most . NET programmerFor example, the attribute is the attribute, get, set, and so on. How can this problem be solved? However, Java programmerThey want to encapsulate fields like this. Public void Set_Name (int value) {this. _ name = value;} public int Get_Name () {return this. _ name;} found no? They use a method. So what's the problem ?. NET? The answer is no !! Through the decompilation tool, we find that the attribute get set is used. Finally, the compilation tool will compile the above two methods. "And.. NET properties in GET, SET, or even simplified writing, all belong. NET syntax sugar category, Microsoft simplifies our writing, let. NET developers are more convenient to use!"

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.