简体   繁体   English

值类型Vs参考类型 - 对象类C#

[英]Value Type Vs Reference Type - Object Class C#

如果值类型和引用类型来自作为引用类型的对象类型,那么值类型如何值类型和引用类型是引用它们都来自引用类型。

Basically, it is a cheat ;-p 基本上,这是一个骗子;-p

Any struct (ie anything inherited from ValueType ) is treated with value-type semantics. 任何struct (即从ValueType继承的任何struct )都使用值类型语义进行处理。 But there is a boxing conversion to object as necessary; 但是必要时会有一个拳击转换object ; meaning that if you cast a struct to an object , it will create a special object (on the managed heap) containing the data (as a clone) from your value*. 这意味着如果将struct转换为object ,它将在您的值*中创建一个包含数据(作为克隆)的特殊对象(在托管堆上)。

The boxed version is a reference-type. 盒装版本是参考类型。 You can unbox this (by casting) back to the struct version, which reverses this (copies the clones data from the object on the heap into your local value). 您可以将此(通过强制转换)取消装回到struct版本,从而将其反转(将克隆数据从堆上的对象复制到本地值)。


*=unless it is an empty Nullable<T> , which boxes to null ; * =除非它是一个空的Nullable<T> ,它将为null ; likewise, null unboxes to an empty Nullable<T> . 同样, null unboxes到一个空的Nullable<T>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM