首页 > 其他 > 详细

装箱与拆箱

时间:2014-04-12 22:05:38      阅读:537      评论:0      收藏:0      [点我收藏+]

Because unboxed value types don’t have a sync block index, you can’t have multiple threads syn-chronize their access to the instance by using the methods of the System.Threading.Monitor type
(or by using C#’s lock statement).
Even though unboxed value types don’t have a type object pointer, you can still call virtual meth -ods (such as Equals , GetHashCode , or ToString ) inherited or overridden by the type. If your value
type overrides one of these virtual methods, then the CLR can invoke the method nonvirtually be -cause value types are implicitly sealed and cannot have any types derived from them. In addition, the
value type instance being used to invoke the virtual method is not boxed. However, if your override
of the virtual method calls into the base type‘s implementation of the method, then the value type
instance does get boxed when calling the base type‘s implementation so that a reference to a heap
object gets passed to the this pointer into the base method.
However, calling a nonvirtual inherited method (such as GetType or MemberwiseClone) always
requires the value type to be boxed because these methods are defined by System.Object , so the
methods expect the this argument to be a pointer that refers to an object on the heap.
In addition, casting an unboxed instance of a value type to one of the type’s interfaces requires the
instance to be boxed, because interface variables must always contain a reference to an object on the
heap. 

装箱与拆箱,布布扣,bubuko.com

装箱与拆箱

原文:http://www.cnblogs.com/mdfxp/p/3660682.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!