CLR 是怎样去处理Boxing and Unboxing Value Types?

It's possible to convert a value type to a reference type by using a mechanism called boxing.
Internally, here's what happens when an instance of a value type is boxed:
1. Memory is allocated from the managed heap. The amount of memory allocated is the size
required by the value type's fields plus the two additional overhead members (the type
object pointer and the sync block index) required by all objects on the managed heap.
2. The value type's fields are copied to the newly allocated heap memory.
3. The address of the object is returned. This address is now a reference to an object; the
value type is now a reference type.

Unboxing is not the exact opposite of boxing. The unboxing operation is much less costly
than boxing.
Internally, here's exactly what happens when a boxed value type instance is unboxed:
1. If the variable containing the reference to the boxed value type instance is null,
a NullReferenceException is thrown.
2. If the reference doesn't refer to an object that is a boxed instance of the desired value
type, an InvalidCastException is thrown

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值