C # Proverbs use attributes to access private members of a class

Source: Internet
Author: User

In a program, it is unavoidable to access the private members of an object. There are two ways to implement this type of functionality before, the simplest of which is to change the member accessors from "private" to "public", and the other is to provide publicly accessible member access functions. So now write a program in C #, you no longer need to take the two methods mentioned above, and directly using the attributes to complete.

First, let's look at how the three methods are implemented and invoked, and here is an example of accessing the private member strname of the "EmployeeInfo" class, as shown in the following table.

The
  private string StrName; access method
Private string Strnam E;

is:

public string strName;

employeeinfo empnew ...;
String strnamevalue = Empnew.strname;
Empnew.strname = "Me";
add the following two member functions: common S Tring GetName ()
{
return strName;
}
public void SetName (string Name)
{
StrName = name;
}
employeeinfo empnew ...;
String strnamevalue = Empnew.getname ();
Empnew.setname ("Me");
property Add the following properties: public string Name
{
get{return strName;}
set{strName = value;}
}
employeeinfo empnew ...;
String strnamevalue = Empnew.name;
Empnew.name = "Me";

So what's the difference between the three methods, the following table, you can better illustrate the difference between the three.

TD style= "BORDER:0;PADDING:0;" > > Second only to the first method
  code complexity code efficiency
simple highest
public member function > no damage security tedious, and calls not directly minimum
no break security simple

(Note: This indicates the worst of each subkey in red)

Therefore, you can see that using attributes does not destroy the encapsulation of the class, does not weaken the security of the Code, and is as simple as the first method, but slightly less efficient than the first method. But generally speaking, using attributes in C # to access the private members of a class is not the second choice.

However, for the use of attributes and as mentioned in the table above, some questions are inevitably raised.

Question one: is the use of attributes to achieve the effect of member functions, that is, complete complex code operations.

In fact, the underlying implementation of the property is the use of member functions, but this part of the conversion is done by the system, so when writing properties, as a member function, that is, in the member function can write code snippets, can be applied in the attribute. The Microsoft intermediate Language (MSIL) code that is converted by the attribute is posted below.

.property instance string Name()
{
 .get instance string NameSpace.EmployeeInfo::get_Name()
 .set instance void NameSpace.EmployeeInfo::set_Name(string)
}// end of property EmployeeInfo::Name
.method public hidebysig specialname instance string get_Name() cil managed
{
 ...
}// end of method EmployeeInfo::get_Name
.method public hidebysig specialname instance void set_Name(string 'value') cil managed
{
 ...
}// end of method EmployeeInfo::set_Name

As above is the intermediate language code converted by the Name property of the previous EmployeeInfo class (but omitting the specific implementation code of the function, since this is not intended to be a study of intermediate language code, if you need to know more about this part, refer to the intermediate language books).

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.