ArticleDirectory
- 1. 1. Follow the object-oriented Principle
- 1.2 elastic
Attribute advantages:
- Follow the object-oriented Principle
- Elastic
1. 1. Follow the object-oriented Principle
Attributes enable us to expose data members as public interfaces and provide us with the expected encapsulation in the object-oriented environment. It looks like a data member on the surface, but it is implemented internally in a way.
Public ClassTestclass
{
//Member variables
Public StringName;
}
Public ClassTestclass
{
//Attribute
Public StringName {Get;Set;}
}
Testclass= NewTestclass ();
Testclass. Name= "Testclass";
This sectionCodeVery concise and intuitive. Some people think that if there is a need in the future, replace the data member name of the testclass class with the attribute OK, and the code using the testclass type does not need to be changed. This is true to some extent.
The attribute does not seem to be different from the data member when it is accessed. This is exactly the goal of C # introducing new property syntax. However, the attribute is not data after all. The access attribute and access data produce different msil. InProgramAfter compilation, the compiler automatically compiles get/set in the property into two methods when compiling the program into msil, so the property can get all the benefits of the function. However, we can use the following msil analysis to identify the differences between member variables and attributes.
Figure 1 msil after testclass Compilation
In Figure 1, we did not find the name attribute and replaced it with two get_name/set_name methods, which fully demonstrates that the attribute is called by method.
Figure 2 member variable msil
Figure 3 attribute msil
Note that although the access attribute is the same as the access data member C #Source codeBut the C # compiler converts them into different il code.
In other words, although the attributes and data members are compatible at the source code level, they are not compatible at the binary level. This means that if you change a type of public data member to a public attribute, we must re-compile all C # code that uses this public data member.
1.2 elastic
Modifying attributes is more flexible than modifying member variables. For example, when a program wants to determine whether a member variable is empty, we need to add the judgment logic somewhere in the code, however, we only need to add the judgment logic to the attribute, saving the trouble of searching for the logic to be added.
Private String _ Name;
Public String Name
{
Get
{
If (_ Name = Null )
Return String . Empty;
Return _ Name;
}
Set
{
_ Name = Value;
}
}
Because attributes are implemented using methods, it is easier to add multi-threaded support for them-simply provide access control for synchronous data in the get and set methods:
Public String Name
{
Get
{
Lock ( This )
{
Return _ Name;
}
}
Set
{
Lock ( This )
{
_ Name = Value;
}
}
}
Experience summary:
As long as we want to expose data to public or protected interfaces of the type, we should use attributes for implementation. Indexes should be used for types with sequence or dictionary features.