Share via


Boxing 和 Unboxing (C# 程式設計手冊)

更新:2010 年 7 月

Boxing 處理是將實值型別轉換成 object 型別,或是任何由這個實值型別實作的介面型別。 當 CLR Box 處理實值型別時,它會將值包裝在 System.Object 中,並儲存在 Managed 堆積上。 Unbox 處理則會從物件擷取實值型別。 Boxing 是隱含的;unboxing 是明確的。 boxing 和 unboxing 的概念是 C# 型別系統統一檢視的基礎,其中可將任何型別的值視為物件。

在下列範例中,會以「Box」處理整數變數 i,然後將此變數指派給物件 o。

int i = 123;
// The following line boxes i.
object o = i;  

物件 o 接著即可經 Unbox 處理,並指派給整數變數 i:

o = 123;
i = (int)o;  // unboxing

下列範例說明如何在 C# 中使用 boxing。

// String.Concat example.
// String.Concat has many versions. Rest the mouse pointer on 
// Concat in the following statement to verify that the version
// that is used here takes three object arguments. Both 42 and
// true must be boxed.
Console.WriteLine(String.Concat("Answer", 42, true));


// List example.
// Create a list of objects to hold a heterogeneous collection 
// of elements.
List<object> mixedList = new List<object>();

// Add a string element to the list. 
mixedList.Add("First Group:");

// Add some integers to the list. 
for (int j = 1; j < 5; j++)
{
    // Rest the mouse pointer over j to verify that you are adding
    // an int to a list of objects. Each element j is boxed when 
    // you add j to mixedList.
    mixedList.Add(j);
}

// Add another string and more integers.
mixedList.Add("Second Group:");
for (int j = 5; j < 10; j++)
{
    mixedList.Add(j);
}

// Display the elements in the list. Declare the loop variable by 
// using var, so that the compiler assigns its type.
foreach (var item in mixedList)
{
    // Rest the mouse pointer over item to verify that the elements
    // of mixedList are objects.
    Console.WriteLine(item);
}

// The following loop sums the squares of the first group of boxed
// integers in mixedList. The list elements are objects, and cannot
// be multiplied or added to the sum until they are unboxed. The
// unboxing must be done explicitly.
var sum = 0;
for (var j = 1; j < 5; j++)
{
    // The following statement causes a compiler error: Operator 
    // '*' cannot be applied to operands of type 'object' and
    // 'object'. 
    //sum += mixedList[j] * mixedList[j]);

    // After the list elements are unboxed, the computation does 
    // not cause a compiler error.
    sum += (int)mixedList[j] * (int)mixedList[j];
}

// The sum displayed is 30, the sum of 1 + 4 + 9 + 16.
Console.WriteLine("Sum: " + sum);

// Output:
// Answer42True
// First Group:
// 1
// 2
// 3
// 4
// Second Group:
// 5
// 6
// 7
// 8
// 9
// Sum: 30

效能

相對於單純的指派,Box 和 Unbox 處理是會耗費大量運算資源的處理序。 當實值型別經 Box 處理時,必須配置及建構新的物件。 Unbox 處理所需的轉換雖較為簡單,但也同樣需要大量運算資源。 如需詳細資訊,請參閱效能

Boxing

Boxing 可用來儲存記憶體回收堆積中的實值型別。 Boxing 是一種隱含轉換,可將實值型別轉換成 object 型別,或是任何由這個實值型別實作的介面型別。 對實值型別進行 Boxing 作業時,會在堆積上配置物件執行個體,並將值複製至新物件中。

考慮下列實值型別變數的宣告:

int i = 123;

下列陳述式隱含地對變數 i 進行 boxing 作業:

// Boxing copies the value of i into object o.
object o = i;  

這個陳述式的結果是在堆疊中建立物件參考 o,會參考堆積中型別為 int 的值。 這個值是指派給變數 i 之實值型別值的複本。 兩個變數 i 和 o 之間的差異會在下圖示範。

Boxing 轉換

BoxingConversion 圖形

您也可以執行明確的 boxing 作業,如下列範例所示,但明確的 boxing 作業從來都不是必要的:

int i = 123;
object o = (object)i;  // explicit boxing

描述

這個範例會使用 Boxing,將整數變數 i 轉換為物件 o。 接著,儲存在變數 i 中的值會從 123 變更為 456。 這個範例顯示原來的實值型別及 boxed 物件是使用獨立的記憶體位置,因此可以儲存不同的值。

範例

class TestBoxing
{
    static void Main()
    {
        int i = 123;

        // Boxing copies the value of i into object o.
        object o = i;  

        // Change the value of i.
        i = 456;  

        // The change in i does not effect the value stored in o.
        System.Console.WriteLine("The value-type value = {0}", i);
        System.Console.WriteLine("The object-type value = {0}", o);
    }
}
/* Output:
    The value-type value = 456
    The object-type value = 123
*/

在下列程式碼中,將示範 Unbox 處理無效並產生 InvalidCastException 的案例。 程式碼中也會使用 try 和 catch,以在發生錯誤顯示錯誤訊息。

class TestUnboxing
{
    static void Main()
    {
        int i = 123;
        object o = i;  // implicit boxing

        try
        {
            int j = (short)o;  // attempt to unbox

            System.Console.WriteLine("Unboxing OK.");
        }
        catch (System.InvalidCastException e)
        {
            System.Console.WriteLine("{0} Error: Incorrect unboxing.", e.Message);
        }
    }
}

這個程式會輸出:

Specified cast is not valid. Error: Incorrect unboxing.

如果您將陳述式:

int j = (short) o;

更改為:

int j = (int) o;

就會執行轉換,而且您會得到輸出:

Unboxing OK.

Unboxing

Unbox 處理是將 object 型別明確轉換成實值型別,或從介面型別明確轉換成實作介面的實值型別。 Unboxing 作業包含:

  • 檢查物件執行個體以確定這是指定之實值型別的 boxed 值

  • 將執行個體的值複製到實值型別變數

下列陳述式示範了 boxing 和 unboxing 作業:

int i = 123;      // a value type
object o = i;     // boxing
int j = (int)o;   // unboxing

下圖示範上述陳述式的結果。

Unboxing 轉換

UnBoxing 轉換圖形

若要在執行階段成功以 Unbox 處理實值型別,要以 Unbox 處理的項目必須是物件的參考,而且物件是先前該實值型別的執行個體經 Box 處理時所建立的物件。 嘗試 Unbox null 造成 NullReferenceException。 嘗試 Unbox 不相容之實值型別的參考,造成 InvalidCastException

C# 語言規格

如需詳細資訊,請參閱 C# 語言規格。 語言規格是 C# 語法和用法的決定性來源。

相關章節

如需詳細資訊,請參閱下列主題:

C# 語言規格

如需詳細資訊,請參閱 C# 語言規格。 語言規格是 C# 語法和用法的決定性來源。

請參閱

概念

C# 程式設計手冊

變更記錄

日期

記錄

原因

2010 年 7 月

釐清簡介並加入範例。

客戶回函。