none
Boxing and Un-Boxing in .Net RRS feed

Answers

  • No boxing/unboxing takes place. The ToString method returns a new string value representing i. Nothing happens to i itself.

    Sunday, May 19, 2013 5:04 PM
  • But it is not i being boxed and returned a new string is being created and returned.

    Sunday, May 19, 2013 5:11 PM
  • Boxing is changing the representation of value type to reference one, but internally type it still preserved.

    For example, when you create an int variable, it just takes 4 bytes (on stack in case of local variable). This is a primitive unboxed representation of value type. When you box it by casting to reference type, a new object is allocated on heap, and its size is larger (about 12 bytes for 32-bit platforms, and even more for 64-bit ones):

    1. TypeRef - pointer to original System.Type, so its value is typeof(int).
    2. SyncObjIdx - index of synchronisation object, in case you use Monitor lock with boxed object as an argument (not actually relevant for our case).
    3. Value itself - copied from unboxed variable.

    When being unboxed, CLR will allocate new 4 bytes (on stack in case of local variable) and copy Value into it, but first check if boxed TypeRef is really typeof(int) to guarantee type safety, otherwise unboxing will throw an exception.

    In your case the new type is string, while the original type is int. This couldn't be boxing by definition, as boxing can produce only an object of base type (System.Object or one of implemented interfaces), otherwise it will throw an exception.

    Monday, May 20, 2013 12:02 AM
  • Hi,
    int i = 6;

    Does calling i.Tostring() can be considered as a Boxing Operation?


    Note that Int32 overrides ToString() method, so ToString is not called from base Object class. Hence No Boxing occurs here.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Monday, May 20, 2013 4:49 AM

All replies

  • No boxing/unboxing takes place. The ToString method returns a new string value representing i. Nothing happens to i itself.

    Sunday, May 19, 2013 5:04 PM
  • Yes, But string is a reference type.
    Value Type i is converted to Reference type string.
    This is boxing ,as per the Definition of Boxing.
    Sunday, May 19, 2013 5:09 PM
  • But it is not i being boxed and returned a new string is being created and returned.

    Sunday, May 19, 2013 5:11 PM
  • Boxing is changing the representation of value type to reference one, but internally type it still preserved.

    For example, when you create an int variable, it just takes 4 bytes (on stack in case of local variable). This is a primitive unboxed representation of value type. When you box it by casting to reference type, a new object is allocated on heap, and its size is larger (about 12 bytes for 32-bit platforms, and even more for 64-bit ones):

    1. TypeRef - pointer to original System.Type, so its value is typeof(int).
    2. SyncObjIdx - index of synchronisation object, in case you use Monitor lock with boxed object as an argument (not actually relevant for our case).
    3. Value itself - copied from unboxed variable.

    When being unboxed, CLR will allocate new 4 bytes (on stack in case of local variable) and copy Value into it, but first check if boxed TypeRef is really typeof(int) to guarantee type safety, otherwise unboxing will throw an exception.

    In your case the new type is string, while the original type is int. This couldn't be boxing by definition, as boxing can produce only an object of base type (System.Object or one of implemented interfaces), otherwise it will throw an exception.

    Monday, May 20, 2013 12:02 AM
  • Hi,
    int i = 6;

    Does calling i.Tostring() can be considered as a Boxing Operation?


    Note that Int32 overrides ToString() method, so ToString is not called from base Object class. Hence No Boxing occurs here.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Monday, May 20, 2013 4:49 AM