[C #] C # knowledge Review,

Source: Internet
Author: User
Tags unpack

[C #] C # knowledge Review,
Packing and unpacking directory

  • Performance
  • Packing
  • Unpack

 

Introduction

Packing is to convert the value typeobjectType or any interface type implemented by this value type. When the CLR loads the value type, the value is encapsulated into System. Object, and then stored on the managed stack. The value type is extracted from the object. Packing is implicit; unpacking is explicit. The concept of packing and unpacking is the basis of the unified view of type system C #. Any type of value is regarded as an object.

In the following exampleiPacked and allocated to objectsobj.

1 static void Main (string [] args) 2 {3 var I = 123; // System. int324 5 // bind to I (implicit) 6 object obj = I; 7 8 Console. read (); 9}

 

Then, You can unpack the object obj and assign it to the integer variable.i。

1 static void Main (string [] args) 2 {3 var I = 123; // System. int32 4 5 // pack (implicitly) 6 object obj = I; 7 8 // unpack (explicitly) obj 9 I = (int) obj; 10 11 Console. read (); 12}

 

Here we use the code to demonstrate the packing and unpacking operation:

1 static void Main (string [] args) 2 {3 // use string. format indicates the usage of packing. Here, 24 is used for packing 4 var formatStr = string. format ("{0} {1 }. "," I'm ", 24); 5 Console. writeLine ($ "formatStr: {formatStr}"); 6 7 var objs = new List <object> (); 8 for (int I = 0; I <5; I ++) 9 {10 // each time I is packed into objs 11 objs. add (I); 12} 13 14 Console. writeLine ("============"); 15 16 foreach (var obj in objs) 17 {18 // two object types cannot be used directly *, you need to use int for explicit box unboxing 19 Console. writeLine ($ "{obj} * {obj} = {(int) obj * (int) obj}"); 20} 21 22 Console. read (); 23}

Performance

Compared with the simple assignment, the packing and unboxing process requires a lot of calculations. When packing value types, a new object must be allocated and constructed. The forced conversion required for unpacking also requires a large amount of calculations, but the degree is lighter. If your operations are in the center of a loop, you will obviously feel performance problems.

 

Packing

Packing is used to store value types in the heap. Packing is a value typeobjectType or any interface type implemented by this value type. Binning the value type will allocate an object instance in the heap and copy the value to the new object.

See the Declaration of the following value type variables:

    var i = 123;    //System.Int32

The following statementsiThe packing operation is implicitly applied:

// Pack I (implicitly) into object obj = I;

  

The result of this statement is to create an object reference on the stack.oAnd is referenced on the stack.intType value. This value is assigned to the variable.iA copy of the Value Type value. Two variables are described.iAndo.

1 int I = 123; // Value Type 2 object o = I; // boxed 3 int j = (int) o; // unpack

C # basic Review Series

C # knowledge Review-serialization

C # knowledge Review-Expression Tree Expression Trees

C # knowledge Review-feature Attribute and AssemblyInfo. cs-understanding common feature attributes

C # knowledge Review-delegated delegate and C # knowledge Review-delegated delegate (continued)

C # knowledge Review-Introduction to events and C # knowledge Review-Event Events

There is no unspeakable secret between the string, the String, the big S, and the small S

 

 

[Blogger] Anti-Bot

[Source] http://www.cnblogs.com/liqingwen/p/6189128.html

[Reference] Microsoft official documentation

 

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.