Packing and unpacking

Mainly related to performance loss

Boxing is to turn the value type in the stack into an instance of object and put it in the heap. Then store the address of the instance in the stack.

Unboxing is to move the data of the instance in the heap into a value type and put it in the stack

int x=100;//The following is the memory situation, It is placed in the stack

share picture

object obj;//

share picture

obj=x;//This sentence first open up an address in Heap, put 100 in the stack, and then put the address 000000113 The original obj references the memory of the parameter, and the boxing is complete.

share picture

int y=(int)obj;//Unboxing, the value in the heap is moved to the stack as required, that is, int

Share Picture

Leave a Comment

Your email address will not be published.