"C #" Boxing and unpacking

Source: Internet
Author: User

From the point of view of memory execution, the value type of memory is allocated on the stack of threads, whereas the memory of the reference type is allocated on the managed heap. Therefore, conversions from value types to reference types are bound to involve operations such as copying of data and reference to pointers.

Boxing operations, the approximate procedure is to allocate the memory of the new object in the managed heap, copy the field of the value type into that memory, and then return the address of the object, thus completing the transition from the value type to the reference type, and the container to get the address of the boxed object from the value type part field. Boxing and unpacking are not completely symmetric reciprocal operations, and unpacking does not contain a copy of the field.

Concept mined area:

    1. Boxing and unpacking are not exactly reciprocal reciprocal operations. From the memory point of view, the performance cost of unpacking is much smaller than boxing, but in the actual execution, after unpacking is often accompanied by a copy of the field, in C # as an example, the compiler will always automatically generate a copy of the field after the unboxing.
    2. Only boxed objects can be disassembled, not all reference types. Casting a non-boxed reference type to a value type throws a InvalidCastException exception.
Split

Value types, which provide a lightweight data structure with less memory overhead and a significant effect on system performance. The disadvantage is that the default method table pointer cannot invoke a value type on a method that expects System.Object or its inheriting class.

Packing process parsing

    1. Memory allocation: Allocates memory space in the managed heap, with memory size plus additional memory space for the size of the boxed value type, mainly including method table pointers and Syncblockindex, which are used by two members for CLR management reference type objects.
    2. Instance copy: Copy the field of the value type to the newly allocated memory
    3. Address return

Unpacking process parsing

    1. Instance check: First check whether it is null, if NullReferenceException is thrown, if not, check the object instance, ensure that it is a boxed value of the given value type, and that the type after unpacking is the same type as the original. Otherwise it throws InvalidCastException
    2. The pointer returns: Returns the address of the boxed object that belongs to the original value type part of the field. The additional Member: The method table pointer is not visible to the Syncblockindex pointer.
    3. Field copy: Copies the fields from the instance in the managed heap to the thread's stack.
Performance
    1. Notice the possibility of implicit boxing in the actual project, and provide several overloaded methods to avoid the occurrence of boxing.
    2. Boxing and unpacking are often implicit, and it is a good choice to improve performance by explicitly implementing boxing operations in the system.
    3. Generics can effectively reduce the occurrence of packing and unpacking, and greatly improve the performance and stability of the system.
Application
  1. ArrayList and Array
  2. Hashtable
  3. Enumeration
    The enumeration type is typical. NET value type, can be boxed into system.object,system.valuetype and System.Enum, and the three interface types implemented by System.Enum System.icomparable,system.iconvertible,system.ifor Mattable
  4. Pay attention to casual implicit conversions
    1  Public Static voidMain ()2 {3     inti = -;4     //Packing5 I.gettype ();6     //not boxed7 i.tostring ();8     //Explicit Boxing9     Objecto =i;TenHashtable HT =NewHashtable (); OneHt. ADD (" One", O); AHt. ADD (" Both", O); -}

    The GetType method is provided by the System.Object type, so a boxing operation must be performed when the value type is called, and the ToString method is overridden by the int type and therefore is not boxed. The Add method of Hashtable accepts parameters of type System.Object, so implicit boxing operations are reduced by explicit type conversions.

"C #" Boxing and unpacking

Related Article

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.