值类型与引用类型 - 对象类 C# [英] Value Type Vs Reference Type - Object Class C#

查看:20
本文介绍了值类型与引用类型 - 对象类 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果值类型和引用类型都来自引用类型的对象类型,那么当它们都来自引用类型时,值类型如何是值类型和引用类型是引用.

if Value Types and Reference Type are from Object Type which is a reference type, then how value type is value type and reference type is reference when they all come from refernce type.

推荐答案

基本上就是个骗子;-p

Basically, it is a cheat ;-p

任何 struct(即从 ValueType 继承的任何东西)都用值类型语义处理.但是有必要时装箱转换object;这意味着如果您将 struct 转换为 object,它将创建一个特殊对象(在托管堆上),其中包含来自您的 value* 的数据(作为克隆).

Any struct (i.e. anything inherited from ValueType) is treated with value-type semantics. But there is a boxing conversion to object as necessary; 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 版本,这将反转此操作(将堆上对象的克隆数据复制到您的本地值中).

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

*=除非它是一个空的Nullable,这些框为null;同样,null 拆箱为空的 Nullable.

*=unless it is an empty Nullable<T>, which boxes to null; likewise, null unboxes to an empty Nullable<T>.

这篇关于值类型与引用类型 - 对象类 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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