Property vs Attribute-reprinted

Source: Internet
Author: User
Differences caused by two "attributes"-differences between property and attribute
Although this is not as serious as a "blood case caused by a steamed bun", it is enough to become the second technical blind spot for. Net programmers.
Property and attribute are both called "attributes" because of negligence in the translation field of domestic IT books.
In fact, they come from two different fields. attribute belongs to the concept of OOA/OOD, and property belongs to the concept of programming language. Below we will illustrate their similarities and differences.
Attribute
Attributes is the metadata of the Microsoft. NET Framework file. It can be used to describe your code to the runtime, or affect the behavior of the application when the program is running.
Property
Attribute is the basic concept of object-oriented programming. It provides access encapsulation for private fields. In C #, The get and set accessors are used to perform operations on readable and writable attributes, provides secure and flexible data access encapsulation. The concept of attributes is not the focus of this article, and we believe that most technical staff should have a clear concept of attributes. The following are simple attributes:
Differences
It can be said that there is no comparability between the two, except that the language characteristics of our country lead to different differences. In fact, as long as we remember that attributes is derived from the system and attributes class, its main role is to describe, for example, in a custom control, "attribute" refers to attribute,
For example
[Dllimport ("user32.dll")]

Attribute also has many system "default" attributes, as shown in the following table.
Predefined attribute Effective Target description
Attributeusage class specifies the effective usage of another attribute class
Clscompliant indicates whether the program element is compatible with Cls.
Conditional method indicates that if no correlated string is defined, the compiler can ignore any call to this method.
Dllimport method specifies the DLL location that contains the implementation of external Methods
Stathread method (main) indicates that the default thread model of the program is Sta.
Mtathread method (main) indicates that the default program model is multi-thread (MTA)
In addition to assembly, module, parameter, and return, obsolete marks an element as unavailable, notifying users that this element will be used by future products.
Paramarray parameter allows a single parameter to be implicitly treated as a Params (array) parameter
Serializable class, struct, Enum, and delegate specify that all public and private fields of this type can be serialized.
Nonserialized field is applied to Fields marked as serializable classes, indicating that these fields will not be serializable
Structlayout class, struct specifies the nature of the data layout of a class or structure, such as auto, explicit, or sequential
Threadstatic field (static) implements local thread storage (TLS ). A given static field cannot be shared across multiple threads. Each thread has a copy of this static field.

Property is a field in the programming process, that is, a member of the class.
For example:
Private int hour; // defines the private variable to indicate "Hour", which cannot be accessed externally .}

Public int hour // defines the hour Program Interface

{

Set {hour = value ;}

Get {return hour ;}

}

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.