有关装箱/拆箱的查询 [英] Query regarding Boxing/Unboxing

查看:60
本文介绍了有关装箱/拆箱的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中:

In the below code:

object Wed1 = 8;
object Wed2 = Wed1;
Wed1 = 10;
Console.WriteLine("Wed1 : " +Wed1.ToString());
Console.WriteLine("Wed2 : " +Wed2.ToString());



输出为:

星期三:10
周三2:8

为什么不是

星期三:10
周三2:10

即,为什么不将其视为引用类型值?



Output is:

Wed1 : 10
Wed2 : 8

Why isn''t it

Wed1 : 10
Wed2 : 10

ie, why isn''t it treated like reference type values?

推荐答案

您正在显示输出原样,然后询问为什么输出不完全正确照原样.

我认为您需要更改问题.

而且它们不是引用对象,因为您没有引用它们,只是将它们输出到consol.

它们是值类型,而不是引用类型.

引用类型 [值类型 [
You are showing the output is as it is and then asking why it isn''t exactly as it is.

I think you need to alter the question.

And they are not reference objects because you haven''t referenced them, merely outputted to consol.

They are Value Types, not Reference Types.

Reference Types[^]

Value Types[^]

Check them out.


一个很好的问题.

Object是.Net Framework中类Object的别名.
尽管Object本身是一个类,所以将其视为ref.类型,但在将任何值分配给Object类的Object时,它在内部和之后装值,因此不再是引用.类型.

请参见
[
A very good question.

Object is a Alias for a Class Object in .Net Framework.

though Object itself is a class so treated as a ref. type but while assigning any value to Object of class Object, It boxes value inside and after that it''s no more a ref. type.

See THIS[^] you will get better idea.


这篇关于有关装箱/拆箱的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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