[C #] C # knowledge Review,
Packing and unpacking directory
- Performance
- Packing
- Unpack
Introduction
Packing is to convert the value typeobject
Type 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 examplei
Packed 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 typeobject
Type 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 statementsi
The 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.o
And is referenced on the stack.int
Type value. This value is assigned to the variable.i
A copy of the Value Type value. Two variables are described.i
Ando
.
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