Boxing and unboxing)

Source: Internet
Author: User

First, we will introduce boxing) And unpacking (Unboxing.. NetThere are two types: Value Type and reference type. The essential difference between the two types is that value-type data is distributed in the stack, while reference-type data is distributed in the stack. If you want to put a value type data on the stack, You need to pack the data. Otherwise, you need to unpack the value type data on the stack.

For example, for the following simple packing and unpacking operation statements.

IntI = 123;

ObjectOBJ = I;// Boxing

 

If(OBJIs Int)

IntJ = (Int) OBJ;// Unboxing

 

To better interpret the packing and unpacking operations, I borrowedMsdnAbout"BoxingIs as follows.

Now that I understand the meaning of these two terms, I want to explain why we need to reduce the number of packing and unpacking operations.

There are two reasons, mainly about efficiency: one is the low efficiency of heap operations, and the other is the GC for memory resources allocated on the heap to reduce the efficiency.ProgramEfficiency.

 

Considering these two factors, we need to reduce the number of packing and unpacking operations in the program.

How can we reduce it? The two operations involve formatting output operations, such as statements such as string. Format and console. writeline.

For example:

Console. writeline ("Number list: {0}, {1}, {2 }",

1, 2, 3 );

 

For "1, 2, 3", it is equivalent to the previous "123" and requires two operations: packing and unpacking. So how to avoid it? In fact, you only need to pass the reference type data to writeline, that is, the following method.

Console. writeline ("Number list: {0}, {1}, {2 }",

1. tostring (), 2. tostring (), 3. tostring ());

 

Because the result of "1. tostring ()" is of the string type and belongs to the reference type, the packing and unpacking operations are not involved.

 

Secondly, many binning and unpacking operations are involved in the collection, such as arraylist or hashtable.

Putting value-type data in a collection may cause potential errors. For example:

Public struct person

{

Private string _ name;

Public string name

{

Get {return _ name ;}

Set {_ name = value ;}

}

 

Public Person (string personname)

{

_ Name = personname;

}

 

Public override string tostring ()

{

Return _ name;

}

 

}

 

Arraylist arrpersons = new arraylist ();

Person P = new person ("oldname ");

Arrpersons. Add (P );

P = (person) arrpersons [0];

P. Name = "newname ";

 

Debug. writeline (person) arrpersons [0]). Name); // It's "oldname"

 

This problem is actually in frontArticle. Someone may say whether the modification can be done as follows.

(Person) arrpersons [0]). Name = "newname"; // can't be compiled

 

Unfortunately, the above operations cannot be compiled. Why? For "(person) arrpersons [0])", the system uses a temporary variable to receive the value type data after unpacking, therefore, because the value type is assigned to the stack, the operation is an entity operation, but the system does not allow modification to a temporary value type data.

Arraylist arrpersons = new arraylist ();

Person P = new person ("oldname ");

Arrpersons. Add (P );

// Try to change the name

P = (person) arrpersons [0];

P. Name = "newname ";

 

Arrpersons. removeat (0); // remove old data first

Arrpersons. insert (0, P); // Add new data

Debug. writeline (person) arrpersons [0]). Name); // It's "newname"

 

In fact, this operation will produce excessive packing and unpacking operations. In this way, you can use interfaces to reduce the number of packing and unpacking operations. The interface implementation in this example should be as follows.

Public interface ipersonname

{

String name {Get; set ;}

}

 

Public struct person: ipersonname

{

Private string _ name;

Public string name

{

Get {return _ name ;}

Set {_ name = value ;}

}

 

Public Person (string personname)

{

_ Name = personname;

}

 

Public override string tostring ()

{

Return _ name;

}

}

 

Arraylist arrpersons = new arraylist ();

Person P = new person ("oldname ");

Arrpersons. Add (P );

// Change the name

(Ipersonname) arrpersons [0]). Name = "newname ";

 

Debug. writeline (person) arrpersons [0]). Name); // It's "newname"

 

Many people ask why the value type cannot be modified, that is

(Person) arrpersons [0]). Name = "newname"; // can't be compiled

The above interface type can be modified, that is

(Ipersonname) arrpersons [0]). Name = "newname ";

This is because the types of the generated temporary variables are different. The former has been described earlier, and the latter belongs to the reference type because the generated temporary variables are of the ipersonname type, it is equivalent to a temporary variable that is referenced by the original object. Therefore, modifications to the temporary variable are directly modified to the original object. It can be said that the difference here is that the types of temporary objects are different, resulting in the essential difference.

Rewrite through the interface, which reduces the boxing and unpacking operations and ensures the correctness of the modification. However, it should be noted that the interface here is of the reference type. If the interface accesses or returns a value type, it can be implemented through the interface, but for the packing and unpacking operations, not reduced.

The packing and unpacking operations are basically finished. Remember that frequent packing and unpacking operations will reduce the program efficiency, so avoid writing as much as possible.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/songxiaozhao/archive/2008/04/01/2235012.aspx

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.