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