C # programming guide
How to: declare and use read/write attributes (C # programming guide)Attributes can provide convenience for public data members, without the risk of being unprotected, uncontrolled, and unauthenticated access to object data. This is achieved through the "accessor": The accessor is a special method for assigning values to and retrieving values for basic data members. You can use the set accessors to assign values to data members and use the get accessors to retrieve the values of data members. This example demonstrates the person class, which has two attributes: Name (string) and age (INT ). Both attributes are provided
GetAnd
SetAccessors, so they are considered read/write attributes.
ExampleC # copy the code class person {privatestring m_name = "N/A"; privateint m_age = 0; // declare a Name property of Type string: publicstring name {get {return m_name ;} set {m_name = value; }}// declare an age Property of Type INT: publicint age {get {return m_age;} set {m_age = value ;}} public override string tostring () {return "name =" + name + ", age =" + age;} class testperson {staticvoid main () {// create a new person object: person = new person (); // print out the name and the age associated with the person: system. console. writeline ("person details-{0}", person); // set some values on the person object: person. name = "Joe"; person. age = 99; system. console. writeline ("person details-{0}", person); // increment the age property: person. age + = 1; system. console. writeline ("person details-{0}", person );}}
OutputPerson details-name = N/A, age = 0 person details-name = Joe, age = 99 person details-name = Joe, age = 100
Reliable ProgrammingIn the preceding example, the name and age attributes are public and contain
GetAnd
SetAccessors. This allows any object to read and write these attributes. However, it is sometimes necessary to exclude one of the accessors. For example, omitted
SetThe accessor will make this attribute read-only: C # copy the code publicstring name {get {return m_name ;}}. In addition, you can also publish an accessor, make another accessor private or protected. For more information, see asymmetric accessors. After declaring attributes, you can use these attributes as if you were using class fields. This allows you to use a natural syntax to obtain and set attribute values, as shown in the following statement: C # copy the code person. name = "Joe"; person. age = 99; Note: Attributes
SetYou can use a special value variable. This variable contains user-specified values, for example, C # copy the code m_name = value; Note the concise syntax used to increase the age attribute on the person object: C # copy the code person. age + = 1; if
SetAnd
GetIf the method is used for model attributes, the equivalent code may be similar to: copy the code person. setage (person. getage () + 1). In this example,
TostringMethod: C # copy the code public override string tostring () {return "name =" + name + ", age =" + age;} note that the program is not explicitly used
Tostring. By default
WritelineTo call. (Source: msdn)