C # packing and unpacking

Source: Internet
Author: User
Tags mscorlib

 

C #Packing and unpacking

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

CTS (Common Type System), one of the important technologies and foundations of NET ). As the name suggests, CTS is a general type system that exists to realize the rules that must be followed when applications declare and use these types .. . 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.

The Code is as follows:

Using System; namespace Box {////// abstract description of BoxAndUnBox. /// Public class BoxAndUnBox {public BoxAndUnBox () {// TODO: add the constructor logic //}/////////////////////////////// //////////////////////////////////////// ///// // static void Main (string [] args) {double box1 = 11.222; // define a value-form 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

The MSIL code is as follows:

. Method private hidebysig static void Main (string [] args) cel managed {. 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 // The IL_0000 to IL_000a is the IL_000b: ldloc.0 IL_000c: box [mscorlib] System that defines the value type variable. double IL_0011: stloc.1 // line IL_000b to IL_0011 describes the IL_0012 of the object objBox = box1 code: 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) divide the stack memory, and allocate the memory on the stack = box1 size + space occupied by the objBox and its structure; (2) box1 value (11.222) copied to the recently allocated stack;

(3) The address stack assigned to the objBox is pointed 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 {///// abstract description of BoxAndUnBox. /// Public class BoxAndUnBox {public BoxAndUnBox () {// TODO: add the constructor logic //}/////////////////////////////// //////////////////////////////////////// ///// // static void Main (string [] args) {double box2 = 11.222; object objBox = box2; double d1_box = (double) objBox; // unpack the referenced object and return the Console value. 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 abstract description. /// Public class BoxAndUnBox {public BoxAndUnBox () {// TODO: add the constructor logic //}/////////////////////////////// /// // static void Main (string [] args) {double box1 = 11.222; object objBox = box1; double d1_box = (double) objBox; object objUnBox = d1_box; Console. writeLine ("The Value is '{0}' and The UnBoxed is {1}", objBox, objUnBox );} //////////////////////////////////////// ///////////////////////////}}

C #Packing and unpacking

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

CTS (Common Type System), one of the important technologies and foundations of NET ). As the name suggests, CTS is a general type system that exists to realize the rules that must be followed when applications declare and use these types .. . 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.

The Code is as follows:

Using System; namespace Box {////// abstract description of BoxAndUnBox. /// Public class BoxAndUnBox {public BoxAndUnBox () {// TODO: add the constructor logic //}/////////////////////////////// //////////////////////////////////////// ///// // static void Main (string [] args) {double box1 = 11.222; // define a value-form 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

The MSIL code is as follows:

. Method private hidebysig static void Main (string [] args) cel managed {. 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 // The IL_0000 to IL_000a is the IL_000b: ldloc.0 IL_000c: box [mscorlib] System that defines the value type variable. double IL_0011: stloc.1 // line IL_000b to IL_0011 describes the IL_0012 of the object objBox = box1 code: 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) divide the stack memory, and allocate the memory on the stack = box1 size + space occupied by the objBox and its structure; (2) box1 value (11.222) copied to the recently allocated stack;

(3) The address stack assigned to the objBox is pointed 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 {///// abstract description of BoxAndUnBox. /// Public class BoxAndUnBox {public BoxAndUnBox () {// TODO: add the constructor logic //}/////////////////////////////// //////////////////////////////////////// ///// // static void Main (string [] args) {double box2 = 11.222; object objBox = box2; double d1_box = (double) objBox; // unpack the referenced object and return the Console value. 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 abstract description. /// Public class BoxAndUnBox {public BoxAndUnBox () {// TODO: add the constructor logic //}/////////////////////////////// /// // static void Main (string [] args) {double box1 = 11.222; object objBox = box1; double d1_box = (double) objBox; object objUnBox = d1_box; Console. writeLine ("The Value is '{0}' and The UnBoxed is {1}", objBox, objUnBox );} //////////////////////////////////////// ///////////////////////////}}

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.