Based on the implementation method of attributes, attributes can be divided into automatically implemented attributes and general attributes.
Conventional attributes require specific people to implement get accessors or set accessors, and a field usually needs to correspond to it; the get and set accessors of automatically implemented attributes are omitted, and corresponding fields are not required in the code.
General attributes:
Public class person {// ----------------------- // common attributes // ----------------------- private string name; // <summary> name </Summary> Public string name {get {return this. name;} set {This. name = value ;}}}
Automatically implemented attributes:
Public class person {// ----------------------- // automatically implemented attributes // ------------------------- // <summary> name </Summary> Public string name {Get; set ;}}
You can add the access permission modifier (private, protected, internal) to the get or set accessors of the automatically implemented attribute to control the access permission of this attribute.
(* Note that the access permission of the access permission modifier added to the get or set accessors must be smaller than the access permission of the overall access attribute of the get or set accessors)
Read-only or write-only access modifier added to get or set accessors:
Public class person {// ----------------------- // The automatically implemented attribute is read-only // ------------------------- // <summary> name </Summary> Public string name {Get; private set ;}}
Public class person {// ----------------------- // The automatically implemented attributes are written only // ------------------------- // <summary> name </Summary> Public string name {private get; set ;}}
Pay attention to the following points for automatically implemented attributes:
① Set accessors and get accessors must be implemented simultaneously;
② For automatically implemented attributes, the compiler will automatically generate a private field at runtime, and this automatically generated field cannot be directly accessed;
③ Automatic attribute cannot be used to verify data validity or perform other special processing.