General understanding C #(2 attributes)

Source: Internet
Author: User

2. Attributes

Attribute is a familiar concept for users of Delphi and Visual Basic. The purpose of using properties is to normalize the concept of the accessor/setter [Note: the original is getter/setter], which is a widely used mode, especially in RAD (Quick Application Development) tools.

The following are typical code that you may write in Java or C ++:

Foo. setSize (getSize () + 1 );

Label. getFont (). setBold (true );

The same Code in C # may become:

Foo. size ++;

Label. font. bold = true;

C # code is more intuitive and readable for users who use foo and label. The implementation attributes are almost the same:

Java/C ++:

Public int getSize ()

{

Return size;

}

Public void setSize (int value)

{

Size = value;

}

C #:

Public int Size

{

Get {return size ;}

Set {size = value ;}

}

C # provides a fresh way to process read/write attributes. In C #, The get and set methods are internal, while in Java and C ++, manual maintenance is required. C # has many advantages. It encourages programmers to think about attributes-which of the following attributes is more natural to mark as readable and read-only? Or shouldn't it be an attribute at all? If you want to change the name of your attribute, you only need to check one location. (I have seen the accessors and seters separated by several hundred lines of code: in C ++ (Java), it refers to the accessor and setter for the same data member/field (generally ]). Only one annotation is needed, which avoids synchronization between the two annotations. IDE [: integrated development environment] can help with this (in fact, I suggest they do this [Note: "They" here should be related to Microsoft personnel ]), however, we should keep in mind a basic principle of programming-to do our best to simulate the abstraction of our problem space. A language that supports attributes will help you get better abstraction.

[Note: an objection to the property advantage is that when using this syntax, you cannot determine whether to manipulate a field or attribute. However, in Java (including C #, of course), almost all classes that are really complicated do not have public fields. Generally, fields only have the smallest possible access level (private/protected, or the default value defined by the language), and are exposed only by means of the accessor and setter, this also means you can get beautiful syntaxes. It is also feasible for the IDE to parse the code. Different colors can be used to highlight the property, or provide Code Completion information to indicate whether it is an attribute. We should also see that if a class is well designed, users of this class will only care about the interfaces (or specifications) of this class, for its derived classes, it may also be the protected method and attribute (C ++/Java does not have the explicit attribute concept). The customers here include its derived classes ], instead of its internal implementation. Another possible argument is that attributes are inefficient. In fact, a good compiler can return only the accessor of a field inline, which is as fast as directly accessing a field. After all, even if you use a field that is more effective than the accessor or setter, you can use the attribute as follows-you can change the attribute field in the future: it refers to the part of the implementation code that can be changed by the accessor or setter, for example, changing the field operated by the accessor setter, you can also perform some verification or modification work in the accessor/setter without affecting the code dependent on this attribute]

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.