这些是装箱/拆箱示例吗

发布于 2024-11-13 10:49:25 字数 531 浏览 6 评论 0原文

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);

我认为在所有示例中,技术上都是相同的。

  1. 在堆栈上创建值类型
  2. 在堆栈上创建引用,并将值复制到堆。
  3. 堆值被复制到引用。引用被删除。

所以我猜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.

  1. A value type is created on the stack
  2. A reference is created on the stack, the value is copied to the heap.
  3. The heap value is copied to the reference. The reference gets deleted.

So I guess 2 und 3 are examples for boxing/unboxing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏末染殇 2024-11-20 10:49:25

在所有三个示例中:

iBoxedi 的盒装副本。

在示例 2 中:
这里不涉及拆箱,因为 ToString 是一个虚拟方法,最终会解析为 int.ToString,然后由 int.Parse 解析。 code>,在堆栈上返回一个未装箱的 int

在示例 3 中:
iBoxed 将在方法 Convert.ToInt32 的主体中取消装箱,并再次在堆栈上作为非装箱整数返回。

In all three examples:

iBoxed is a boxed copy of i.

In example 2:
There is no unboxing involved here, as ToString is a virtual method that will finally resolve to int.ToString, which will then be parsed by int.Parse, returning a non-boxed int on the stack.

In example 3:
iBoxed will get unboxed in the body of the method Convert.ToInt32 and returned as a non-boxed integer, on the stack again.

画中仙 2024-11-20 10:49:25

第二个例子是装箱但不是拆箱。 int.parse 不会编译,因为它需要一个字符串,而 iBoxed 是一个对象。我认为你混淆了拳击和转换的概念。装箱实际上是采用一个值类型,即 C 中所说的 POD,并将其视为引用。拆箱是从其容器中提取相同值类型的能力。

例 2. 已修复

int i = 123;  // Assigment 
object iBoxed = i ; // This is boxing 
i = int.parse(iBoxed.toString());  //This is unboxing but only for the twostring method the assignment is a value type copy. 

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

int i = 123;  // Assigment 
object iBoxed = i ; // This is boxing 
i = int.parse(iBoxed.toString());  //This is unboxing but only for the twostring method the assignment is a value type copy. 
笑红尘 2024-11-20 10:49:25

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

━╋う一瞬間旳綻放 2024-11-20 10:49:25

在所有示例中,值类型 i 都被转换为对象。这是根据 MSDN 对拳击的定义。装箱也可以通过以下方式完成:

object boxed = (object)i;

拆箱是将对象转换回值类型。
示例 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:

object boxed = (object)i;

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 a string argument but would be unboxing if you did iBoxed.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文