这些是装箱/拆箱示例吗
2 和 3 是装箱/拆箱示例吗?
1)文档示例:
int i = 123;
object iBoxed = i;
i = (int) iBoxed;
2:装箱/拆箱也是如此吗?
int i = 123;
object iBoxed = i;
i = Int32.Parse(iBoxed.ToString());
3:装箱/拆箱也是如此吗?
int i = 123;
object iBoxed = i;
i = Convert.ToInt32(iBoxed);
我认为在所有示例中,技术上都是相同的。
- 在堆栈上创建值类型
- 在堆栈上创建引用,并将值复制到堆。
- 堆值被复制到引用。引用被删除。
所以我猜2和3是装箱/拆箱的例子?
Are 2 and 3 boxing/unboxing examples?
1) The documentation example:
int i = 123;
object iBoxed = i;
i = (int) iBoxed;
2: Is the boxing/unboxing as well?
int i = 123;
object iBoxed = i;
i = Int32.Parse(iBoxed.ToString());
3: Is the boxing/unboxing as well?
int i = 123;
object iBoxed = i;
i = Convert.ToInt32(iBoxed);
I assume that in all examples technically happens the same.
- A value type is created on the stack
- A reference is created on the stack, the value is copied to the heap.
- The heap value is copied to the reference. The reference gets deleted.
So I guess 2 und 3 are examples for boxing/unboxing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在所有三个示例中:
iBoxed
是i
的盒装副本。在示例 2 中:
这里不涉及拆箱,因为
ToString
是一个虚拟方法,最终会解析为int.ToString
,然后由int.Parse
解析。 code>,在堆栈上返回一个未装箱的int
。在示例 3 中:
iBoxed
将在方法Convert.ToInt32
的主体中取消装箱,并再次在堆栈上作为非装箱整数返回。In all three examples:
iBoxed
is a boxed copy ofi
.In example 2:
There is no unboxing involved here, as
ToString
is a virtual method that will finally resolve toint.ToString
, which will then be parsed byint.Parse
, returning a non-boxedint
on the stack.In example 3:
iBoxed
will get unboxed in the body of the methodConvert.ToInt32
and returned as a non-boxed integer, on the stack again.第二个例子是装箱但不是拆箱。 int.parse 不会编译,因为它需要一个字符串,而 iBoxed 是一个对象。我认为你混淆了拳击和转换的概念。装箱实际上是采用一个值类型,即 C 中所说的 POD,并将其视为引用。拆箱是从其容器中提取相同值类型的能力。
例 2. 已修复
The second example is boxing but not unboxing. The int.parse won't compile because it expects a string and iBoxed is an object. I think you are mixing the concepts of boxing and conversion. Boxing is really about taking a value type ie POD as they say in C and treating it as reference unboxing is the ability to extract that same value type out of its container.
Ex 2. Fixed
object iBoxed = i
负责装箱。示例 2 不起作用,因为
int.Parse
需要一个字符串It's
object iBoxed = i
which do the boxing.Example 2 wont work since
int.Parse
expects a string在所有示例中,值类型
i
都被转换为对象。这是根据 MSDN 对拳击的定义。装箱也可以通过以下方式完成:拆箱是将对象转换回值类型。
示例 1 正在拆箱
示例 2 将无法编译,因为
Parse
需要一个string
参数,但如果您执行iBoxed.ToString()
则会拆箱3 我怀疑不是拆箱,因为
Convert
方法只会返回您传入的值。值得了解装箱和拆箱的性能影响。它比正常的任务更昂贵。
In all your examples, the value type
i
is being converted to an object. This is the definition of boxing according to MSDN. Boxing could also be done by:The unboxing is the conversion of the object back to a value type.
Example 1 is unboxing
Example 2 will fail to compile because
Parse
expects astring
argument but would be unboxing if you didiBoxed.ToString()
Example 3 i suspect is not unboxing because the
Convert
method will just return the value that you pass in.Its worth being aware of the performance hit of boxing and unboxing. It is more expensive than normal assignment.