带有引用类型变量的对象类型装箱 [英] Object type boxing with a reference type variable

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

问题描述

装箱是将值类型分配给对象类型时.将引用类型分配给对象时是否相同?

Boxing is when a value type is assigned to an object type. Is it the same when a reference type is assigned to an object?

分配类型(不是对象)后,会发生什么?那个拳击也是吗?

When a type (which isn't object) is assigned, what happens? Is that boxing too?

    int num=5;
    object obj = num;  //boxing
    //////////////////////
    MyClass my = new MyClass();
    object obj = my; //what is name this convert  (whethere is boxing?)

推荐答案

我假设您的意思是

string s = "hello";
object x = s;        // no boxing, just implict conversion to base-type.

之所以可行,是因为 System.String 与所有其他类一样,是从 System.Object :

This works because System.String, like all other classes, derives from System.Object:

public sealed class String : Object { ... }

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

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