值类型VS参考类型 - 对象类C# [英] Value Type Vs Reference Type - Object Class C#

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

问题描述

如果值类型和引用类型是从哪个是引用类型,那么值类型是如何值类型和引用类型的引用时,他们都来自refernce类型前来对象类型。

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

任何结构(即从值类型继承任何东西)与价值型的语义处理。但是有一个装箱转换对象都是必要的;这意味着,如果你投一个包含结构对象,它会创建一个特殊的对象(在托管堆中)从你的价值*。

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

盒装版是引用类型。您可以拆箱这(通过铸造)回到结构版本,逆转这个(复印件从堆上的对象克隆数据到本地值)。

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


* =除非它是一个空的可空< T> ,这盒;同样, unboxes为空可空< T>

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

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

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