拳击 [英] Boxing

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

问题描述

拳击和拆箱的实际用途是什么?b $ b在什么情况下会被使用。

因为所有类型都是从对象中完全取消的...什么

是需要拳击和拆箱.....帮助

解决方案

SK< an ******* @ discussions.microsoft.com>写道:

拳击和拆箱的实际用途是什么?在什么情况下它会被使用。
因为所有类型都是从对象中完全消失的。
是需要拳击和拆箱.....帮助




装箱和拆箱使得处理价值类型更加容易

期望的是参考类型的情况。例如,

是装箱,它允许我们将一个int添加到一个ArrayList,或者将DataRow中的

值设置为一个浮点数等。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件


" SK" <一个******* @ discussions.microsoft.com>写在

新闻:9d **************************** @ phx.gbl ...

拳击和拆箱的实际用途是什么?在什么情况下它会被使用。
因为所有类型都是从对象中完全消失的。
是需要的拳击和拆箱.....帮助




当然所有的价值类型都是从对象派生出来的,这就是拳击的原因

可以从语言的角度来看。但是(没有装箱)值类型

总是存在于堆栈上并且引用类型总是存在于堆上:如果你想要在堆上获得值类型,那么你需要一个参考类型框。这就是为什么它被称为拳击。


Niki


Niki Estner< ni ********* @ cube.net>写道:

" SK" <一个******* @ discussions.microsoft.com>在
新闻中写道:9d **************************** @ phx.gbl ...

在什么情况下使用拳击和拆箱的实际用途是什么。
因为所有类型都是从对象中完全消失的。所以需要拳击和拆箱。 ....帮助
当然所有的价值类型都是从对象派生出来的,这就是从语言的角度来看可以使拳击成为可能。




那里实际上有一些额外的复杂性。值类型本身

*不是从System.Object派生的。盒装值类型派生自

System.Object(通过System.ValueType)。这两种类型的名称相同。

这有点奇怪 - 请参阅CIL规范第8.2.4节了解更多

信息。


但是,由语言本身来定义它想要实现的拳击

转换。

但是(没有拳击)值类型
总是存在于堆栈中,引用类型总是存在于堆上




不正确。例如:


公共课测试

{

int i = 5;


static void Main()

{

测试t =新测试();

}

}


在Test类型中创建了一个值类型。对象本身

(包含值类型值)在堆上。


参见 http://www.pobox.com/~skeet/csharp/memory.html

-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件。 />


What is the real use of boxing and unboxing
in what situations will it be used.
Since all types are utlmately dereived from object..what
is the need for boxing and unboxing.....help

解决方案

SK <an*******@discussions.microsoft.com> wrote:

What is the real use of boxing and unboxing
in what situations will it be used.
Since all types are utlmately dereived from object..what
is the need for boxing and unboxing.....help



Boxing and unboxing makes it much easier to deal with value types in
situations where what is expected is a reference type. For instance, it
is boxing which allows us to add an int to an ArrayList, or set the
value in a DataRow to a float, etc.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


"SK" <an*******@discussions.microsoft.com> wrote in
news:9d****************************@phx.gbl...

What is the real use of boxing and unboxing
in what situations will it be used.
Since all types are utlmately dereived from object..what
is the need for boxing and unboxing.....help



Of course all value types are derived from object, that''s what makes boxing
possible from a language perspective. But (without boxing) value types
always live on the stack and reference types always live on the heap: If you
want to get a value type on the heap, you need a "reference type box" around
it, that''s why it''s called boxing.

Niki


Niki Estner <ni*********@cube.net> wrote:

"SK" <an*******@discussions.microsoft.com> wrote in
news:9d****************************@phx.gbl...

What is the real use of boxing and unboxing
in what situations will it be used.
Since all types are utlmately dereived from object..what
is the need for boxing and unboxing.....help
Of course all value types are derived from object, that''s what makes
boxing possible from a language perspective.



There''s actually some extra complexity there. The value type itself
*doesn''t* derive from System.Object. The boxed value type derived from
System.Object (via System.ValueType). The two types have the same name.
It''s all a bit odd - see section 8.2.4 of the CIL spec for more
information.

However, it''s up to the language itself to define the boxing
conversions it wants to implement.
But (without boxing) value types
always live on the stack and reference types always live on the heap



Not true. For instance:

public class Test
{
int i = 5;

static void Main()
{
Test t = new Test();
}
}

That has created a value type within the Test type. The object itself
(containing the value type value) is on the heap.

See http://www.pobox.com/~skeet/csharp/memory.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


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

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