Effective C # principle 17: Minimizing the packing and unpacking

Source: Internet
Author: User

Value types are containers of data that do not have much of a nature. On the other hand, it means. NET Framework is designed to be a single inherited reference type, System.Object, as the root object exists throughout the inheritance relationship. The purpose of the two types of design is quite different. NET Framework uses boxing and unboxing to link two different types of data. Boxing is the placement of a value type data on an untyped reference object, so that a value type can be used as a reference type when required. Unpacking is an extra copy of the value type data from the box. Boxing and unboxing allow you to use value type data where you need to use the System.Object object. But boxing and unboxing operations are performance robbers, and at some point boxing and unboxing can produce temporary objects that can cause hidden bugs in the program. You should avoid using boxing and unpacking as much as possible.

Boxing can transform a value type data into a reference type, and a new reference object is created on the heap, which is the "box" where the value-type data stores a copy of the reference type. See Figure 2.3 For a demonstration of how boxed objects are accessed and stored. The box contains a copy of this value type object, and the copy implements the interface of the boxed object. When you want to retrieve any content from this box, a copy of the value type data is created and returned. This is the key concept of boxing and unboxing: a copy of an object is stored in a box, and whenever you visit the box again, another copy is created.

Figure 2.3, the value type data is in the box. Converts a value-type data into a System.Object reference, and an unnamed reference type is created. Value-type data is stored in this nameless reference object, and all access methods go through the box to reach the location of the value type data store.

The most insidious place is this boxing and unboxing is often done automatically! When you use value type data in any place where the desired type is System.Object, the compiler generates a boxed and unboxing statement. In addition, boxing and unboxing occur when you access value-type data through an interface pointer. You don't get any warnings when you box, even the simplest statements. For example, the following:

Console.WriteLine("A few numbers:{0}, {1}, {2}",
 25, 32, 50);

Using the overloaded Console.WriteLine function requires an array reference of the System.Object type, the integer is a value type, and must be boxed to pass to the overloaded WriteLine method. The only way to force these three integers to become System.Object objects is to box them. In addition, inside the WriteLine, the inside of the box is reached by calling the ToString () method on the Box object. In a sense, you generate such a structure:

int i =25;
object o = i; // box
Console.WriteLine(o.ToString());

Inside the WriteLine, the following code is executed:

object o;
int i = ( int )o; // unbox
string output = i.ToString( );

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.