Description: C # Boxing and unpacking

Source: Internet
Author: User


Concept Brief:

Before packing and unpacking, simply say the value type, the reference type:

Value type: Primitive Type (Sbyte,Byte, Short,Ushort,Int,Uint,Long,Ulong,Char,Float,Double,Bool,Decimal), Enumeration (enum), Structure (struct) is allocated memory in the stack and initialized at the same time to ensure that the data is not null;

Reference type: class, array, interface, delegate, string, etc., allocate memory in the heap, initialize to NULL, the reference type needs garbage collection to reclaim the memory, the value type does not need, out of scope, the system will automatically release;

Boxing: Converts a value type to a reference type, which is used to store a value type in the garbage-collected heap, which is an implicit conversion of a value type to an object type or to any interface type implemented by this value type;

Unboxing: Converts a reference type to a value type, from an object type to a value type or from an interface type to an explicit conversion of a value type that implements the interface;


With boxing and unboxing, you can link value types to reference types by allowing any value of a value type to be converted to a value of type Object


Simple example:

int val = 100;
Object obj = val;
Console.WriteLine ("Value of object = {0}", obj);
This is a boxed procedure, which is the process of converting a value type to a reference type

int val = 100;
Object obj = val;
int num = (int) obj;
Console.WriteLine ("Num: {0}", num);
This is a process of unpacking, converting a value type to a reference type, and then converting from a reference type to a value type.

(Note:.) NET, the data type is divided into a value type and a reference (not a pointer to C + +) type , corresponding to this, memory allocation is divided into two ways, one for the stack, and two for the heap (managed heap). Value types are only allocated on the stack, reference types allocate memory to the managed heap, and the managed heap corresponds to garbage collection. )


When to pack

1, call a method with a parameter of type object, which can support any type, when you need to pass a value type (such as Int32), you need to boxing;

2, a non-generic container, the same is to ensure common, and the element type is defined as object. To add value type data to a container, boxing is required;


Carton unboxing Efficiency

Packing:

1, first allocates memory from the managed heap for the newly generated reference object;

2. Then copy the data of the value type into the memory just allocated;

3. Returns the address of the newly allocated object in the managed heap;

Perform a boxing to allocate memory and copy data. These two comparisons affect performance.

Unpacking:

1. First obtain the address of the part of the managed heap that belongs to the value type, which is the strict unboxing;

2. Copy the values from the Reference object to the instance of the value type on the thread stack;

The strict unboxing does not affect performance, but the subsequent operation of the copied data will affect performance as well as the boxing operation.

In theory , when boxing, a new reference object is generated, which can lead to loss of time, which results in reduced efficiency. By overloading functions, you can also avoid boxing by generics.

For the optimization of packing and unpacking code, because the boxed and unboxing are implicit in C #, the fundamental approach is to analyze the code, and the most straightforward way to analyze it is to understand the principle of how to view the anti-compilation Il code. For example, there may be excess boxing in the body of the loop, and you can simply use the pre-packing method to optimize it.

Description: 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.