Box and UnBox what does it means? [duplicate]

☆樱花仙子☆ 提交于 2019-12-21 03:51:42

问题


Possible Duplicates:
Why do we need boxing and unboxing in C#?
What is boxing and unboxing and what are the trade offs?

In C# what doe sit means: "Box and UnBox"?

Here an extract from MSDN where I founded the Text.

But this convenience comes at a cost. Any reference or value type that is added to an ArrayList is implicitly upcast to Object. If the items are value types, they must be boxed when they are added to the list, and unboxed when they are retrieved. Both the casting and the boxing and unboxing operations decrease performance; the effect of boxing and unboxing can be very significant in scenarios where you must iterate over large collections.

Thanks!


回答1:


Here is a more detailed explanation that looks at the internal of Common Language Runtime.

First, let's make the difference between value types and reference types:

  • A value type is held on the stack and a copy of it is passed to called methods
  • A reference value is held in the managed heap and the stack holds only a pointer (reference) to its location. The location, and not the object, is passed to called methods

If you don't know what the stack is (don't be offended), it's a memory area that holds local variables in a method and addresses of caller functions used for return instruction (just to be brief and provide a general answer). When you call a method, a sufficient area on the stack is statically allocated to it, so stack allocation is always called static allocation.

The heap, instead, is a memory area separated from the stack, property of the running process, in which allocation must be first demanded to the operating system, and that's why it's called dynamic allocation (if you don't run in an if statement, for example, memory may not be allocated for your process, instead stack is always allocated).

Just to make a final example on heap and stack: in languages such as C++, declaring int[100] a; statically allocates 100*8 bytes on the stack (64-bit system assumed), while int* a = new int[100]; declares a 8 bytes (on 64-bit systems) area on the stack AND requests 800 more bytes on the heap, if and where available.

Now let's talk about C#:

Boxing

Since int is a value type, and is allocated on the stack, when you cast it to object or any other reference type (actually there is no other reference type from which int can inherit, but it's a general rule) the value must become necessarily a reference type. So a new area on the heap is allocated, the object is boxed inside it and the stack holds a pointer to it.

Unboxing

Just the opposite: when you have a reference type, such as object, and want to cast it to a value type, such as to int, the new value must be held on the stack, so CLR goes to heap, un-boxes the value and copies it to the stack.

In other words

Remember the int[] and int* examples? Simply, when you have int in C#, the runtime expects its stack location to hold the value but instead when you have object, it expects its real value to be in the heap location pointed by the stack.




回答2:


There are two different types in .net Framework.

ValueTypes such as int, double, single

ReferenceTypes ArrayList List and many, many more

Variables of type ValueTypes are stored in Stack ReferenceTyped variables are stored in heap

Variables of type ValueTypes store the VALUE ReferenceTyped variables store the REFERENCE to a value

so if you copy a ValueType variable - there is a real copy of a value but if you copy a ReferenceType variable - you will get an additional reference to the SAME variable.

Boxing in your question means, that a valueType Variable (e.g. int) will be handled liked a reference Type Variable - .net gives it into a new box. So it will be encapsulated within the heap and there will be reference(s) to it.

In case you want the value to be again in a valueType Variable you have to unbox it (take it out of the box). So the value will be taken out of the heap - and stored/given to the stack once again.




回答3:


ArrayList stores only Objects. For a reference-type (like String) this is no problem but for a ValueType (int, DateTime, ..) it is.

These valuetypes need to be converted to an object before you can store them as plain Object. This "converting to object" is called "boxing" and takes a little bit of time.

When you read the value back, you need to convert from Object to int (or whatever it was). This is called "unboxing".



来源:https://stackoverflow.com/questions/4794700/box-and-unbox-what-does-it-means

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!