×

Loading...
Ad by
  • 予人玫瑰,手有余香:加拿大新天地工作移民诚聘求职顾问&行业导师!
Ad by
  • 予人玫瑰,手有余香:加拿大新天地工作移民诚聘求职顾问&行业导师!

Boxing and Unboxing | Unboxing Conversion

本文发表在 rolia.net 枫下论坛Boxing is an implicit conversion of a value type to the type object or to any interface type implemented by this value type. Boxing a value of a value allocates an object instance and copies the value into the new object.

Consider the following declaration of a value-type variable:

int i = 123;
The following statement implicitly applies the boxing operation on the variable i:

object o = i;
The result of this statement is creating an object o, on the stack, that references a value of the type int, on the heap. This value is a copy of the value-type value assigned to the variable i. The difference between the two variables, i and o, is illustrated in the following figure.

Boxing Conversion



It also possible, but never needed, to perform the boxing explicitly as in the following example:

int i = 123;
object o = (object) i;
Example
This example converts an integer variable i to an object o via boxing. Then the value stored in the variable i is changed from 123 to 456. The example shows that the object keeps the original copy of the contents, 123.

// boxing.cs
// Boxing an integer variable
using System;
class TestBoxing
{
public static void Main()
{
int i = 123;
object o = i; // Implicit boxing
i = 456; // Change the contents of i
Console.WriteLine("The value-type value = {0}", i);
Console.WriteLine("The object-type value = {0}", o);
}
}
Output
The value-type value = 456
The object-type value = 123更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 刚学DOT NET ,请教个问题
    1.
    为什么C#语言要提供BOXING 和UNBOXING 功能,有什么特别的意义吗?
    • This is because garbage collection. Boxing is the way that system keeps track how much memory need to be freed later.
      • No, Boxing has nothing to do with garbage collection. Boxing enable a basic data type to have OO ability.
        • No, you are wrong. Boxing as nothing to do with OO. Boxing means that the system allocate space in heap and copy data from stack to managed memory in heap.
          • 他说的没错呀, 要不然MS这么别扭搞这么个BOXING为什么?
          • 数据在stack本来不需要garbage collection,微软硬要把它移到heap然后再回收,微软有病啊
            • sometimes using an object instead of a value type is necessary.
    • boxing--The conversion of a value type instance to an object, which implies that the instance will carry full type information at run time and will be allocated in the heap.
      The Microsoft intermediate language (MSIL) instruction set's box instruction converts a value type to an object by making a copy of the value type and embedding it in a newly allocated object.
    • Boxing and Unboxing | Unboxing Conversion
      本文发表在 rolia.net 枫下论坛Boxing is an implicit conversion of a value type to the type object or to any interface type implemented by this value type. Boxing a value of a value allocates an object instance and copies the value into the new object.

      Consider the following declaration of a value-type variable:

      int i = 123;
      The following statement implicitly applies the boxing operation on the variable i:

      object o = i;
      The result of this statement is creating an object o, on the stack, that references a value of the type int, on the heap. This value is a copy of the value-type value assigned to the variable i. The difference between the two variables, i and o, is illustrated in the following figure.

      Boxing Conversion



      It also possible, but never needed, to perform the boxing explicitly as in the following example:

      int i = 123;
      object o = (object) i;
      Example
      This example converts an integer variable i to an object o via boxing. Then the value stored in the variable i is changed from 123 to 456. The example shows that the object keeps the original copy of the contents, 123.

      // boxing.cs
      // Boxing an integer variable
      using System;
      class TestBoxing
      {
      public static void Main()
      {
      int i = 123;
      object o = i; // Implicit boxing
      i = 456; // Change the contents of i
      Console.WriteLine("The value-type value = {0}", i);
      Console.WriteLine("The object-type value = {0}", o);
      }
      }
      Output
      The value-type value = 456
      The object-type value = 123更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • 2 cents
      boxing transfers a value type to reference type, Unboxing does the reverse.

      Why needs reference type?

      .NET class library needs reference type to pass parameters to methods. Such as:

      For managed C++ code:
      System.Console.WriteLine( " This is a test of output a int value, i={0}", __box(100) );

      However, C# and VB.NET don't need explicitly Boxing on value types because they are converted automatically.

      Hope helps.
    • 有没有visual studio .Net?能不能借来刻一下呢?谢谢!
      • CD-R does not work (you will have problem when you are installing). If you need the original CDs, please let me know. belsay20@yahoo.ca