Are 2 and 3 boxing/unboxing examples?
1) The documentation example:
int i = 123;
object iBoxed = i;
i = (int) iBoxed;
2: Is the boxing/unboxing as well?
int i = 123;
object iBoxed = i;
i = Int32.Parse(iBoxed.ToString());
3: Is the boxing/unboxing as well?
int i = 123;
object iBoxed = i;
i = Convert.ToInt32(iBoxed);
I assume that in all examples technically happens the same.
- A value type is created on the stack
- A reference is created on the stack, the value is copied to the heap.
- The heap value is copied to the reference. The reference gets deleted.
So I guess 2 und 3 are examples for boxing/unboxing?
In all three examples:
iBoxedis a boxed copy ofi.In example 2:
There is no unboxing involved here, as
ToStringis a virtual method that will finally resolve toint.ToString, which will then be parsed byint.Parse, returning a non-boxedinton the stack.In example 3:
iBoxedwill get unboxed in the body of the methodConvert.ToInt32and returned as a non-boxed integer, on the stack again.