. The properties in net

Source: Internet
Author: User

1.What? what is a propertyproperty is the encapsulation of a field. When there is a field in the class, in order to control some of the external performance of this field (for example, accessibility, is read-only?). Write only? Or to do some necessary validation of the self-read assignment, etc.) we privatized this field (private) and set aside a public method for accessing the field. This method is a property. 2.Usage? How to use
For properties,.   NET always provides two ways to define properties. 1)General Definition Method Defines a private field. 1. On the field that needs to be encapsulated CTRL + r  ,  CTRL + E. Properties can be automatically encapsulated. 2. The get and set of attributes can be set with an additional access modifier. private String _name;                                               public string Name {       Get        {              return this._name;       }        Set         {             this._name =value;        }}   2. Simplified notation/*1. This writing is essentially the same as above, and can be seen through reflector. Shorthand, the compiler will automatically generate a private field for us.   2. Because the private field is the compiler plus, in order to ensure that the name is not repeated, the name looks very strange. So in binary serialization, this is a little bit of a hassle. and then again. */public string Name{       get;        set;}   Nature of the 3.Essence attributeWhat is the nature of the attribute? Did you hear this at the beginning of the conversation? For most . NET programmer, attribute is the attribute bai, Get,set bai, how can drop? But for Java Programmer, they want to encapsulate the field as needed. public void Set_name (int value) {this._name= value,} public int get_name () {return this._name;} found?   They are using a method. So what's the problem? What is the difference between net? The answer is NO!! With the anti-compilation tool, we found that we used the property get set. Finally, the compiler tool compiles the two methods as above. "and the Get, SET, and even simplified notation in. NET properties belong to. NET syntax sugar, Microsoft simplifies our writing and makes it easier for. NET Developers! "

. The properties in net

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.