Binning and unboxing value types

Source: Internet
Author: User

Value type is a type that is more lightweight than the reference type, because it does not need to be allocated as an object in the managed heap and will not be garbage collected or referenced by pointers.

         Static   Void Main ()
{
System. Collections. arraylist list = New System. Collections. arraylist ();
Point P; // Allocate a point, which is not allocated in the heap.
For ( Int I = 0 ; I < 20 ; I ++)
{
P. x = P. Y = I; // Initialize a member of the Value Type
List. Add (P ); // Pack the value type and add the reference to arraylist. C # The Compiler automatically generates the il needed to pack a value-type instance.Code, C # The Compiler detects that a value type is passed to a method that needs to be referenced, so code is automatically generated to pack the object. During running, fields in the current point value type instance P will be copied to the newly allocated point object. The address of the packed Point Object (which is already a reference type) is returned to the add method. The point object will remain in the heap until it is reclaimed. Point Value Type Variable P can be reused, because arraylist does not know anything about it.
 
Point P1 = (point) list [0];// Unpack,
 
}
}

The list. Add () method requires an object parameter. Add needs to obtain a reference (or pointer) to an object on the managed stack as a parameter. P is passed in the code, which is a value type. To make the code work correctly, point has been packed once.

An internal event occurs when a value-type instance is boxed:

1. allocate memory in the managed heap.

2. Copy the value type field to the newly allocated heap memory.

3. The address of the returned object. Now this address is a reference to an object, and the value type is now a reference type.

What happened when unpacking:

1. Obtain the address of each field of the boxed object.

2. Copy the value contained in the field from the heap to the stack-based value type instance.

The cost of unpacking is much lower than that of packing. Unpacking is the process of getting a pointer. the pointer contains the original value type in an object. In fact, the Pointer Points to the unpacked part of the boxed instance, so it is different from packing, the binning operation does not require any bytes to be copied in the memory. A field copy operation is usually performed immediately after the binning operation.
 

Binning and unboxing will applyProgramThe speed and memory consumption have adverse effects. Therefore, you should pay attention to when the compiler generates code to automatically perform these operations, and try to manually write the code to avoid automatically generating code.

Static VoidMain ()
{
Int32 I =5;
ObjectOBJ = I;//Packing
Int16 n = (int16) OBJ;//An invalidcastexception is thrown when the binning fails. If the referenced object is not a boxed instance of the expected value type, this exception is thrown.
Int16 nn = (int16) (int32) OBJ;//Split the box first to the correct type and perform the transformation.
Int32 d = (int32) OBJ;//
Console. readkey ();
}

When disassembling an object, you can only convert it to a value type that was not originally packed.

        Static   Void Main ()
{
Int32 I = 5 ;
Object OBJ = I; // Packing
I = 123 ;
Console. writeline (I + " , " + (Int32) OBJ ); // How many times of packing has occurred? Writeline () needs to get a String object, but there is no string object Currently, so take
// Merge the data in some way to create a String object.
// To create a String object, the compiler generates code to call the static Concat method of the string object.
// The string. Concat method has several overloaded versions. All Versions perform the same operation, but the number of parameters varies.
// Connect Three data items to create a string, so the compiler selects
// Public static string Concat (Object obj0, object obj1, object obj2)
// The first parameter is passed with I, which is an unboxed value type.
// Obj1 is a string object reference. Obj3 requires a single unpacking operation. Then pack the data.
Console. writeline (I. tostring () + " , " + OBJ ); // There is no packing operation here. I call the tostring method and return a string. The String object is already of the reference type, so it can be passed directly to the Concat method without any packing operation.
Console. readkey ();
}

Looking at FCL, we will find that many methods are overloaded for different value type parameters. The only purpose of reloading most methods is to reduce the number of packing operations of the constant value type.

Unboxed value types are more lightweight than reference types because of the following two reasons:

1. The value type is no longer hosted on the stack.

2. There is no additional member for each object on the Value Type Stack: A type object pointer and a synchronized block index.

Because the value type does not synchronize block indexes, you cannot use the methods of the system. Threading. Monitor type to achieve synchronization.

In C #, only the interface is used to modify a field in the boxed value type. For details, see page 126.

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.