如何"拳击"在.NET中工作? [英] How does "boxing" work in .NET?

查看:101
本文介绍了如何"拳击"在.NET中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道什么是拳是:

object myBox = 5;

现在我希望增加我的知识。对于此拳操作创建一个特殊类型?或者是 System.Object的用吗?如何.NET处理拳击?

Now I wish to increase my knowledge. Is a special type created for this boxing operation? Or is System.Object used? How does .NET handle boxing?

推荐答案

有没有特殊类型;装箱的值类型是运行时的实现细节。但要理解它的最简单的方法就是想象,有一种特殊类型:

There is no special type; a boxed value type is an implementation detail of the runtime. But the easiest way to understand it is to imagine that there is a special type:

class Box<T> where T : struct
{
    T value;
}

当键入盒&LT; T&GT; 也实现了所有的方法,接口等,T,不管是什么牛逼恰好是。因此,举例来说,你可以想像,盒&LT; INT&GT; 有一个方法ToString这只是调用int.ToString上的值,并将结果返回

Where the type Box<T> also implements all the methods, interfaces, and so on, of T, for whatever T happens to be. So, for example, you could imagine that Box<int> has a method ToString which simply calls int.ToString on the value, and returns the result.

拳击是一个简单的机制以获取参考的东西,是引用类型没有。你只要使周围的事物的盒子,并得到一个参考框。

Boxing is simply a mechanism for getting a reference to something that is not of reference type. You just make a box around the thing and get a reference to the box.

这篇关于如何&QUOT;拳击&QUOT;在.NET中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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