Are these boxing / unboxing examples

Are 2 and 3 examples of boxing / unboxing?

1) Example documentation:

int i = 123;
object iBoxed = i;
i = (int) iBoxed;

2: Also have boxing / unboxing?

int i = 123;
object iBoxed = i;
i = Int32.Parse(iBoxed.ToString());

3: Also have boxing / unboxing?

int i = 123;
object iBoxed = i;
i = Convert.ToInt32(iBoxed);

I assume that in all examples the same thing happens technically.

  • Value type is created on the stack
  • A link is created on the stack, the value is copied to the heap.
  • The heap value is copied to the link. The link is deleted.

So, I think 2 and 3 are examples for boxing / unboxing?

+5
source share
4 answers

In all three examples:

iBoxedis a boxed copy i.

2: unboxing, ToString - , int.ToString, int.Parse, int.

3: iBoxed Convert.ToInt32 .

+7

- , . Int.parse , , iBoxed . , . , POD, C, unboxing - .

Ex 2.

int i = 123;  // Assigment 
object iBoxed = i ; // This is boxing 
i = int.parse(iBoxed.toString());  //This is unboxing but only for the twostring method the assignment is a value type copy. 
+2

object iBoxed = i, .

2 , int.Parse

+1

i . MSDN. :

object boxed = (object)i;

Unboxing - .
1 -

2 , Parse string, , iBoxed.ToString()

3: , , Convert , .

. .

0

All Articles