Packing and unpacking

Source: Internet
Author: User

C # packing and unpacking (unboxing)Boxing implicitly converts a value type to a reference type object. Unboxing is to convert a referenced object into a value type. It is usually used to unpack the packed variable.

Binning and unboxing enable the value type to be considered an object. Binning the value type package it into an instance of the object reference type. This allows the value type to be stored in the garbage collection heap. Unboxing extracts the value type from the object. In this example, the integer variable I is "boxed" and assigned to the object o.

Int I = 1; object o = 5; // boxing, which can be implicitly converted

Then, the object o can be unboxed and assigned to the integer variable I: I = (INT) O; // unboxing at this time I = 5, unboxing is to display forced type conversion

 

Related Knowledge: C #ProgramIs running the programCodeAfter being loaded to the memory, the variables and methods in the program are different in the memory storage area, which helps the program to run more efficiently. C # value types include Enum, structure, Int, float, and short, which are stored in the stack, the garbage collection is not required for the value type to recycle the occupied memory. The system will automatically release the instance when the instance is out of scope. The reference type (generally only the class type) allocates memory in the heap, And the KY Initialization is null. The reference type requires garbage collection to recycle the memory.

What is heap? What is a stack? Heap: the location of the space allocated by functions such as malloc. The address increases from low to high. STACK: it is the space used for automatic Variable Allocation and function calling. The address is reduced from high to low.

1. Heap-generally assigned and released by the programmer. If the programmer does not release the heap, it may be recycled by the OS at the end of the program. Note that it is different from the heap in the data structure. The allocation method is similar to the linked list, where the storage type is unknown.

2. STACK: the stack zone is automatically allocated and released by the compiler, and stores function parameter values and local variable values. The operation method is similar to the stack in the data structure. It stores some types that know the size, such as int and char.

3. Global (static)-the storage of global variables and static variables is put together, and the initialized global variables and static variables are in one area, uninitialized global variables and uninitialized static variables are in another adjacent area. -The program is released by the system after it ends.

4. Text Constant Area-constant strings are placed here. The program is released by the System

5. program code area-stores the binary code of the function body.

Packing and unpacking is one of the new features of. net2.0. It allows us to change a value type to a reference type, and vice versa.

Well ,? The following figure shows that data of all value types is stored in the stack, and data of all reference types is stored in the stack, the reference address of this reference type is stored in the stack.

Example:

Int x = 0;

Int32 y = new int32 ();

Object O;

O = x; // implicit packing.

O = (int32) y; // boxed display.

En ~, For packing, there is no doubt, either explicit or implicit ).

However, there isAssertion ~!Binning must be displayed rather than implicit. That is:

X = O; // error;

X = (INT) O or X = (int32) O; // right;

En ~, There is another problem, for different types of unpacking. There is such a form:

Int32 x = 5;

Int64 y = 6;

Object O;

O = x; or O = (int32) X;

Y = (int64) O; // It's error.

En ~ I think it is okay to convert an int32 type to int64, but I am wrong. This is not a conversion type. This is a box-breaking process. It tells the compiler how to assign a value to another int64 object in int64 mode. This is obviously wrong. Because there is anotherAssertionsYes: The packing type must be the same as the unpacking type. Instead of implicit conversions. So int32 is used for packing, and int32 must be used for unpacking.

What if I want to convert X to int64? You can perform type conversion after unpacking.

Y = (int64) (int32) O; // The first is binning, and the second is type conversion ..

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.