C # packing and unpacking

Source: Internet
Author: User
ArticleDirectory
    • Feedback

C #Packing and unpacking

To understand packing and unpacking, you must understand CTS and its characteristics.

net CTS (common type system), one of the important technologies and foundations ). As the name suggests, CTS is a general type system that exists to declare and use the rules that must be followed when the Program is used ... Net divides the entire system into two types: Value Type and reference type .., most oo languages have this weakness because their original types do not have a common basis point, so they are not actually object-dependent C ++, instead of object-oriented ... NET environment CTS brings us convenience. First, everything in CTS is an object; second, all objects are from a base class-system. Object type. For more information about system. Object, see Microsoft technical documentation. One of the biggest features of CTS value types is that they cannot be null, and the Value Type Variable always has a value. When a value type variable is passed, the actual value of the variable is passed, rather than the "Reference" of the underlying object ". CTS reference type is like a type-safe pointer, which can be null. If the value is null, no reference or type points to an object. When declaring a variable of the reference type, it is operated on the reference (address) of the variable, not the data.

How can we effectively expand and improve the system performance when using this multi-type system? This is the question discussed today. The Seattle people proposed the box and Unbox ideas. In short, packing is to convert value type to reference type; otherwise, it is to split the box.

Packing process:

Step 1: press a value into the stack;

Step 2: Convert the reference type to the value type;

Step 3 indirectly pushes the value to the stack; Step 4 transfers the value to dashbox.

CodeAs follows:

Using system;
Namespace box
{
///
/// Boxandunbox.
///
Public class boxandunbox
{
Public boxandunbox ()
{
//
// Todo: add the constructor logic here
//
}
//////////////////////////////////////// //////////////////////////////////////// /////
Static void main (string [] ARGs)
{
Double box1 = 11.222; // defines a value-shaped variable.
Object objbox = box1; // bind the variable value to a reference object.
Console. writeline ("the value is '{0}' and the boxed is {1}", box1, objbox. tostring ());
}

}
}

Open ildasm.exe

MsilThe Code is as follows:

. method private hidebysig static void main (string [] ARGs) cel managed
{< br>. entrypoint
// code size: 42 (0x2a)
. maxstack 3
. locals Init ([0] float64 box1,
[1] object objbox)
il_0000: NOP

Il_0001: LDC. R8 11.222
Il_000a: stloc.0 //Il_0000 to il_000a define value-type variables
Il_000b: ldloc.0
Il_000c: Box [mscorlib] system. Double
Il_0011: stloc.1 //Lines il_000b to il_0011 describe object objbox = box1 code
Il_0012: ldstr "the value is '{0}' and the unboxed is {1 }"
Il_0017: stloc.0
Il_0018: Box [mscorlib] system. Double
Il_001d: stloc.1
Il_001e: callvirt instance string [mscorlib] system. Object: tostring ()

Il_0023: Call void [mscorlib] system. Console: writeline (string,
Object,
Object)

Il_0028: NOP
Il_0029: Ret
} // End of method boxandunbox: Main

The process of box1 packing:

(1)Stack memory Division: Memory allocated on the stack = box1 size + space occupied by objbox and its structure; (2) box1 value (11.222) is copied to the recently allocated stack;

(3)The address pressure stack assigned to objbox points to an object type, that is, the reference type.

Unpacking process:

The inverse process of packing. Note the following points: Box time does not require explicit type conversion. In Unbox, type conversion is required. Because objects of the reference type can be converted to any type. The difference between computers and the human brain lies in this! Haha! Type conversion is unavoidable and will be monitored by the CTS Management Center. The standard is based on rules.

The following code:

Using system;
Namespace Unbox
{
///
/// Boxandunbox.
///
Public class boxandunbox
{
Public boxandunbox ()
{
//
// Todo: add the constructor logic here
//
}
//////////////////////////////////////// //////////////////////////////////////// /////
Static void main (string [] ARGs)
{
Double box2 = 11.222;
Object objbox = box2;
Double dashbox = (double) objbox; // unpack the referenced object and return the value
Console. writeline ("the value is '{0}' and the unboxed is {1}", box2, d1_box );
}
//////////////////////////////////////// //////////////////////////////////////// /////
}
}

This Code adds a line of double dashbox = (double) objbox;

The meaning of this Code:

Step 1: press a value into the stack;

Step 2: Convert the reference type to the value type;

Step 3 indirectly press the value on the stack;

Step 4: Pass the value to dashbox.

. Method private hidebysig static void main (string [] ARGs) cel managed
{
. Entrypoint
//Code size 48 (0x30)
. Maxstack 3
. Locals Init ([0] float64 box1,
[1] object objbox,
[2] float64 d1_box)
Il_0000: LDC. R8 77.769999999999996
Il_0009: stloc.0
Il_000a: ldloc.0
Il_000b: Box [mscorlib] system. Double
Il_0010: stloc.1
Il_0011: ldloc.1
Il_0012: Unbox [mscorlib] system. Double
Il_0017: ldind. R8
Il_0018: stloc.2
Il_0019: ldstr "the value is '{0}' and the unboxed is {1 }"
Il_001e: ldloc.0
Il_001f: Box [mscorlib] system. Double
Il_0024: ldloc.2
Il_0025: Box [mscorlib] system. Double
Il_002a: Call void [mscorlib] system. Console: writeline (string,
Object,
Object)
Il_002f: Ret
} // End of method boxandunbox: Main

//

Lines il_0011 to il_0018 describe double d1_box = (double) objbox code.

Describe the situation of objbox in case of unpacking: (1) the environment must first determine the address of the stack pointing to a valid object and whether the object is valid when it is converted to a specified type, if it is invalid, an exception is thrown. (2) If the type conversion is correct, a pointer pointing to the value in the object is returned.

Improvement:

To avoid performance loss caused by unnecessary implicit packing, it is best to pack the value before executing these multi-type overload methods.

Code improvement:

Using system;
Namespace newbu
{
///
/// Boxandunbox.
///
Public class boxandunbox
{
Public boxandunbox ()
{
//
// Todo: add the constructor logic here
//
}
//////////////////////////////////////// ///////////////////////////
Static void main (string [] ARGs)
{
Double box1 = 11.222;
Object objbox = box1;
Double dashbox = (double) objbox;
Object objunbox = djsonbox;
Console. writeline ("the value is '{0}' and the unboxed is {1}", objbox, objunbox );
}
//////////////////////////////////////// ///////////////////////////
}
}

Posted on qunews reading (414) Comments (5) edit the category of the favorites: C #

Feedback # 1st Floor Steel and steel

Yes, it is supported.) view the reply reference.

# Floor 2 Idior

Share an article. View reply reference

# Third Floor W [unregistered users]

Haha, interesting! View reply reference

# 4th floor Aspnetx

Because objects of the reference type can be converted to any type. The difference between computers and the human brain lies in this!

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.