c# - 值类型、引用类型和装箱

标签 c#

我知道有many many涉及该主题的问题,我已全部阅读。但我仍然想知道这一点:

如果 objectreference type所有值类型均为 derived来自object ,那么说像 int 这样的值类型是否准确?也是引用类型?具体来说,如果我写:

int i = 10;

i持有值 10,还是持有值 10 的引用的对象?

或者更准确地说,值类型可以通过装箱过程成为引用类型。

最佳答案

is it accurate to say that a value type such as int is also a reference type? …Or is it more accurate to say that a value type can be a reference type through the process of boxing.

两者都不准确。 int 是一个值类型,句点。它不是“也是一种引用类型”。当装箱时,它不会神奇地变成引用类型。包含它的System.Object是一个引用类型,但内部仍然是值类型 int.

Is i holding the value of 10, or is it an object holding a reference to the value of 10?

见上文。 i 的类型为 int,这是一种值类型,因此该变量包含您分配给它的 10 .

另请参阅What is the difference between a reference type and value type in c#?

关于c# - 值类型、引用类型和装箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47048889/

相关文章:

c# - 如何让 XSD.exe 为我生成适当的类,以便我可以创建一个很好的 XML

c# - Decorator 和 Adapter 模式之间有什么区别?

c# - ReactiveUI加载时执行命令

c# - 在 asp.net 中使用 onclick 方法传递变量

c# - 无法获取默认构造函数

c# - 如果文件存在,解压并覆盖

c# - 在 Epplus (C#) 中创建没有基表的数据透视表

c# - Linq 查询 - 列上不同的数据类型

c# - WiX 安装程序不适用于 vs 2012

c# - 使用基于抽象类的子 EF 实体属性