Write high-quality code to improve the 157 suggestions for C # programs-recommendation 91: visible fields should be restructured into attributes,

Source: Internet
Author: User

Write high-quality code to improve the 157 suggestions for C # programs-recommendation 91: visible fields should be restructured into attributes,

91: visible fields should be restructured into attributes.

The essential difference between a field and an attribute is that the attribute is a method.

View the following Person type:

    class Person    {        public string Name { get; set; }    }

After compilation by the compiler, a private field and two public methods are generated for the attribute Name:

[CompilerGenerated]private string <Name>k__BackingField; [CompilerGenerated]public void set_Name(string value){    this.<Name>k__BackingField = value;}[CompilerGenerated]public string get_Name(){    return this.<Name>k__BackingField;}

It can be seen that the attribute is actually the syntactic sugar that the compiler gives us.

Attributes have the following advantages over fields:

1) You can add code for the attribute. Because attributes are methods, you can perform more refined control over the process of setting or retrieving attributes in the method. For example, add a NameChanged event to the property. Fields alone cannot complete such a function.

2) attributes can support thread security. To ensure that attributes become thread-safe, the type itself can be implemented. To ensure that the field supports thread security, it can only be implemented by the caller.

3) attributes are supported by the VS compiler, and automatic attributes are also implemented. The feature of automatic attributes has been widely used in LINQ, especially in anonymous classes. It can only implement read-only automatic attributes, but does not support fields.

4) from the design perspective, that is, the object-oriented perspective, attribute should also be used for public fields. If you change the field status, the type is not notified. If you change the attribute value, the type is supported.

To sum up, if a visible field exists in a type, it should be reconstructed as an attribute. Of course, if a property is only visible internally and does not involve the above four points, we recommend that you use fields.

 

 

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.