How to: declare and use read/write attributes (C # programming guide)

Source: Internet
Author: User

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)
Related Article

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.