C #: general attributes and automatically implemented attributes

Source: Internet
Author: User

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.

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.