C # Medium Property and attribute the Difference
L property is a way to access a field (member Variable, field) (Set/get)
L property is a language feature introduced in C #, which brings some programming skills in C + + to the position of grammar. This feature is to declare class data members private, and to provide public methods for accessing them.
L property can be said to be an object-oriented concept, providing access encapsulation of private fields, in C # with the get and set accessor methods for the operation of the readable writable properties, provides a secure and flexible data access encapsulation. Like what:
- Public class Robot
- {
- Private string name = ""; Fields: Field
- Public string Name//attribute: property, which encapsulates the field.
- {
- Get { return name;}
- Set {name = value;}
- }
- }
L property refers to a data region that is provided outside the class, and is a smart field with get and set accessors to complete the values and assignments of the field. attribute is a description of the object at compile time or runtime property. The two are essentially different, one is a property, a field to access the class, and the other is an attribute that identifies the additional nature of the class, method, and so on.
is a Attribute , or is Property ?
Attribute and property can be translated into "attributes", some places with attribute to express "attributes", and some places in the use of roperty, beginners often in these two words "lost", and even think that there is no difference between the same. But attribute is not equal to property. What is the difference between the two?
We start with Ooa/ood (object oriented analysis/object oriented design, OO analysis and designs). The use of attribute in Ooa/ood represents an attribute, which refers to the character (feature) of an object. What is the term "attribute" we meet in some programming languages (such as C #, Delphi, etc.), and why is it a property, not a attribute? To understand this, we drag C + + to see. The attribute in Ooa/ood is called the member variable (member variable) in C + + and slowly begins to understand. Member variable in C + + can continue in C #, but with a new salutation: field. Look at a simple example of C #:
public class student
{//Student class
private string name; Called Field (field) in C #, called member variable (member variable) in C + +, Ooa/ood is called attribute (attribute)
}
Let's sort out the following table to see the same concepts in Ooa/ood, C + +, and C #:
Description |
Ooa/ood |
C++ |
. NET (C #)/delphi |
Feature |
Attribute |
Member Variable |
Field |
Operation |
Method |
Member Function |
Method |
The method in Ooa/ood refers to the operation of an object (operation). In C + +, called the member function (member functions), it is called method in C #. To add a salutation to a table (method):
Also can be seen, C + + will attribute called member variable, the method is called member function, in fact, is very appropriate. In essence, it is true that variables are declared and functions are defined. Later (C #, etc.) may feel that such a call is not OO (object oriented, OO), and then made some changes. It is needless to say that the method should be ooa/ood, but attribute is called field, and it is always defective. Because the database has the concept of field, and also translated "fields", it is easy to confuse.
We can now differentiate between the attributes mentioned in Ooa/ood (attribute) and the properties mentioned in C #. Continue with the example above:
public class student
{//Student class
private string name; Called Field (field) in C #, called member variable (member variable) in C + +, Ooa/ood is called attribute (attribute)
public string Name
{//C # called property (attribute)
get {return name;}
set {name = value;}
}
}
Let's just see. Private and Public:property are essentially a pair of get/set methods that enable access control and can therefore be set to public, and in accordance with the Ooa/ood principle, the attribute cannot be set to public, but set to private.
The "Attribute" in C # is completely different from the "Attribute" in Ooa/ood, but a new concept introduced by C # (NET).
The official MSDN explanations are as follows:
The common language runtime allows you to add a descriptive declaration like a keyword, called attributes, that labels elements in your program, such as types, fields, methods, and properties. Attributes and Microsoft. NET Framework file metadata are saved together to describe your code to the runtime, or to affect the behavior of your application as it runs.
Popular attribute is a class, but a class of more special classes, attribute must be written in a pair of parentheses, to handle. NET: serialization, the security features of the program, and so on, have a very different role in. Net.
The difference between property and attribute in C #