Boxing..do我得到它的权利? [英] Boxing..do I get it right?

查看:149
本文介绍了Boxing..do我得到它的权利?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/13055/what-is-boxing-and-unboxing-and-what-are-the-trade-offs">What是装箱和拆箱,什么是权衡?

你好, 从我的理解: 当我给你的值类型数据(参考)类型的对象变量,它被装箱,结果是不实际的参考,因为它指向存储在堆上的价值进行复制。是对的吗? 谢谢

Hi, From my understanding: When I assign data of value type to (reference) type object variable, it gets boxed and the result is not actual reference as it points to copy of the value stored on the heap. Is that right? Thanks

推荐答案

哦,不是的非常的。 (我误解你的岗位上开始使用。)

Well, not quite. (I misread your post to start with.)

结果的的真正参考 - 但它不是指你的原始变量。它指的是一个对象,其中包含您的变量举行的拳击比赛时发生的值的副本堆。特别是,改变你的变量的值不改变,在框的值:

The result is a real reference - but it doesn't refer to your original variable. It refers to an object on the heap which contains a copy of the value your variable held when boxing occurred. In particular, changing the value of your variable doesn't change the value in the box:

int i = 10;
object o = i;
i = 11;
Console.WriteLine(o); // Prints 10, not 11

C#不会让你直接访问箱内的价值 - 你只能得到它拆箱,并采取了副本。 C / CLI,另一方面,允许将单独访问箱内的值,甚至改变。 (您仍然有可能使用C#更改值在一个盒子里 - 例如,如果值类型实现了一些接口和接口方法变异值)

C# doesn't let you have direct access to the value inside the box - you can only get it by unboxing and taking a copy. C++/CLI, on the other hand, allows the value inside the box to be accessed separately, and even changed. (You can still potentially change the value in a box with C# - for example if the value type implements some interface and the interface methods mutate the value.)

通常情况下,引用类型,这会导致拳击是对象,但它可能是一些接口的值类型实现,或者只是 System.ValueType

Often the reference type which causes boxing is "object" but it could be some interface the value type implements, or just System.ValueType.

这篇关于Boxing..do我得到它的权利?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆