Boxing 是將 實值類型 轉換為 object
類型或此實值類型實作的任何介面類型的過程。 當 Common Language Runtime (CLR) 封箱一個值類型時,它會將值包裝在 System.Object 實例中,並將其儲存在受控堆積上。 Unboxing 會從物件擷取實值類型。 Boxing 是隱含的;unboxing 是明確的。 Boxing 和 unboxing 的概念是 C# 類型系統統一視圖的基礎,讓任何類型的值都可以被視為物件。
在下列範例中,整數變數 i
被裝箱並指派給物件 。
int i = 123;
// The following line boxes i.
object o = i;
然後,o
物件可以被拆箱,再指派給整數變數 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
績效
在簡單指派中,裝箱和拆箱是計算成本高昂的過程。 當值類型被封箱時,必須配置並建構新的物件。 在較小的程度下,Unboxing 所需的轉換在計算上也相當昂貴。 如需詳細資訊,請參閱效能。
拳擊
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 轉換影像中:
您也可以如下列範例所示明確地執行 Boxing,但絕對不需要明確 Boxing:
int i = 123;
object o = (object)i; // explicit boxing
範例
這個範例會使用Boxing將整數變數 i
轉換成物件 o
。 然後,儲存在 變數 i
中的值會從 123
變更為 456
。 此範例顯示原始值類型和 Boxed 物件使用不同的記憶體位置,因此可以儲存不同的值。
// Create an int variable
int i = 123;
// Box the value type into an object reference
object o = i; // boxing
// Display the initial values
Console.WriteLine($"Value of i: {i}");
Console.WriteLine($"Value of boxed object o: {o}");
// Modify the original value type
i = 456;
// Display the values after modification
Console.WriteLine("\nAfter changing i to 456:");
Console.WriteLine($"Value of i: {i}");
Console.WriteLine($"Value of boxed object o: {o}");
// Output:
// Value of i: 123
// Value of boxed object o: 123
// After changing i to 456:
// Value of i: 456
// Value of boxed object o: 123
拆箱
Unboxing 是將類型 object
明確轉換為 值類型,或是將介面類型轉換為實作該介面的值類型。 Unboxing 作業包含以下內容:
檢查物件實例以確保它是給定實值型別的封裝值。
將實例中的值複製到實值型別變數。
下列語句示範封箱和開箱操作:
int i = 123; // a value type
object o = i; // boxing
int j = (int)o; // unboxing
下圖示範先前語句的結果:
若要讓實值型別的解封箱在運行時成功,需解封箱的項目必須是先前藉由封箱相同實值型別實例所創建的物件參考。 嘗試解包 null
會導致 NullReferenceException。 試圖將不相容的值類型參考進行拆箱會導致InvalidCastException。
範例
下列範例示範無效的 Unboxing 和產生的 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($"{e.Message} Error: Incorrect unboxing.");
}
}
}
此程式會輸出:
Specified cast is not valid. Error: Incorrect unboxing.
如果您變更陳述式:
int j = (short)o;
變更為:
int j = (int)o;
轉換將會執行,而您將取得輸出:
Unboxing OK.
C# 語言規格
如需詳細資訊,請參閱 C# 語言規格。 語言規格是 C# 語法和使用方式的最終來源。