Replace accessible data members with properties.

Source: Internet
Author: User

Replace accessible data members with properties.

Since version 1.0, c # has made a series of enhancements to its attributes to improve its expressive ability. You can specify different access permissions for setter and geter. At the same time, implicit attributes greatly reduce the workload of declaring attributes, which is not much more difficult than declaring data members. If you declare a Public Member in the type, stop now. Let's compare the advantages and disadvantages of the two:

1. attributes can create interfaces similar to data access, but they are actually called methods, so they can enjoy all the advantages of method calls.

2. When the Customer Code accesses the attribute, it is like accessing the public field. However, the underlying implementation of the attribute accessors can also be freely defined.

3. The data binding class in. Net Framework only supports attributes, but does not support public data members. This applies to all data binding class libraries, including WPF, Windows Forms, and Silverlight. The data binding mechanism uses reflection to find specific attributes of the type:

TextBoxCity. DataBindings. Add ("Text", address, "City"); this code binds the Text property of the textBoxCity control to the City property of the address object.

4. attributes are easier to modify when there are new requirements or behaviors in the future. For example, the customer requires that the Name cannot be blank. If you use an attribute to encapsulate the Name, you only need to modify one field:

Public class Customer {private string name; public string Name {get {return name;} set {if (string. isNullOrEmpty (value) {throw new ArgumentException ("Name cannot be blank", "name") ;}name = value ;}}}

However, if you use a public data member, you need to find the code for setting the name for each part and fix it one by one. Think about how long it will take you.

5. Because attributes are implemented by methods, it is very easy to add multi-threaded support. It is easy to make the following changes in the get and set accessors of the available attributes:

Public class Customer {private object syncHandle = new object (); private string name; public string Name {get {lock (syncHandle) return name;} set {if (string. isNullOrEmpty (value) {throw new ArgumentException ("Name cannot be blank", "name") ;}lock (syncHandle) Name = value ;}}}

6. attributes can have all the language features of the method. For example, attributes can be virtual)

      public virtual string Name        {            get;            set;        }

7. The accessors of the attribute will be compiled into your type as two independent methods. In c #, you can set different access permissions for get and set accessors. In this way, you can precisely control the visibility of data members exposed by properties:

      public virtual string Name        {            get;            protected set;        }

8. The attribute feature is very powerful and is a good improvement. But are you still considering using public data members for implementation and changing them to attributes? This seemingly good strategy doesn't actually work. Let's look at the following code:

 public class Customer    {        public string Name;    }string name=customerOne.Name;customerOne.Name="Jim";

It seems simple and intuitive. You can think that if you change the Name to an attribute in the future, the Code does not need to be modified. However, this answer is not completely correct. Attributes are similar to data members only when accessed. However, different MSIL languages are generated for Attribute access and data access. Therefore, it is very difficult to update a single assembly.

9. Performance Comparison

If you view the generated IL, you may compare the performance of the two. Of course, the attribute is not faster than the access speed of data members, but it is not much worse than it, as long as it does not take a long time to compute or make cross-application calls (such as performing Database Operations) in the attribute accessors ).

Attribute should be used whenever data needs to be exposed in public or protected interfaces. All data members should be private without any exception.

The extra input required to encapsulate a variable into a property does not take much time, but it will bring great convenience for future maintenance.


Why should we use attributes to replace the public protected variable?

If the member variables are made public, there will be a program security risk. Therefore, we generally use attributes to encapsulate member variables. The essence of attributes is the method. This makes it secure and convenient and conforms to the object-oriented concept.

If you use data in Table A to replace the original data in Table B, you can use

B

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.