S
Sam Kong
Hello!
I am trying to understand Boxing and Unboxing.
I think I understand most of them except the following.
One of the points of being able to box is so that virtual functions can
be called on value types, and ToString() is a virtual function on
object, so it would appear that d would be boxed when ToString() is
called. However, we haven't used d in a situation where it would get
converted to an object, so a box isn't required. The compiler knows
that a variable of type DateTime can only be that type (it can't be a
derived type, since there are no derived value types), so it can call
DateTime.ToString() directly and set the "this" reference to point to
the d instance on the stack.
Does that mean that calling a virtual method have nothing to do with
boxing?
TIA.
Sam
I am trying to understand Boxing and Unboxing.
I think I understand most of them except the following.
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp03152001.asp)From Mr. Eric Gunnerson's article
One of the points of being able to box is so that virtual functions can
be called on value types, and ToString() is a virtual function on
object, so it would appear that d would be boxed when ToString() is
called. However, we haven't used d in a situation where it would get
converted to an object, so a box isn't required. The compiler knows
that a variable of type DateTime can only be that type (it can't be a
derived type, since there are no derived value types), so it can call
DateTime.ToString() directly and set the "this" reference to point to
the d instance on the stack.
Does that mean that calling a virtual method have nothing to do with
boxing?
TIA.
Sam