Valid tive C # Clause 1

Source: Internet
Author: User
ArticleDirectory
    • 1. 1. Follow the object-oriented Principle
    • 1.2 elastic

Attribute advantages:

    1. Follow the object-oriented Principle
    2. 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.

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.