Class Structure Member-attributes
1. Describe an object in terms of fields and Methods
2. attribute is actually a way to access a field (essentially a method)
1,
Attributes are defined in a class to provide a flexible mechanism for reading, writing, or calculating private field values. Therefore, an attribute generally has a private field corresponding to the attribute.
For example, the class testclass {string name; // Private field can be accessed only in the middle of the class, and external members cannot access the public string name // attribute, you can read and write the corresponding private field {get {return name;} // The get accessor provides the set {name = value;} // sets the private value }}
Read and Write private fields externally through Properties
Testclass ts1 = new testclass ();
// Assign values to attributes
Ts1.name = "dedeyi ";
// Read the attribute value
Console. writeline (ts1.name );
2. The essence of attributes is method.
For example, the class testclass {string name; // Private field can be accessed only in the middle of the class, and external members cannot access public string get_name () {return name ;} public void set_name (string name) {This. name = Name ;}
Public string name // attribute is open to the outside, which can be used to read and write the corresponding private field {
Get {return name;} // get accessors provide the set {name = value;} // sets the private value }}
Add and retrieve in the original class, set the field name, the compiler will report an error, but we have not defined other get_name (), set_name (), methods ah!
However, through decompilation, we can see that there are two methods in testclass that replace the attributes. These two methods are exactly get_name () and set_name ().