我知道有many many涉及该主题的问题,我已全部阅读。但我仍然想知道这一点:
如果 object
是 reference 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/