Boxing 和 Unboxing (C# 程式設計手冊)Boxing and Unboxing (C# Programming Guide)
Boxing 是將實值型別轉換為 object
類型或是由這個實值型別實作之任何介面類型的程序。Boxing is the process of converting a value type to the type object
or to any interface type implemented by this value type. 當 common language runtime (CLR)方塊為實值型別時,它會將值包裝在實例內, System.Object 並將其儲存在受控堆積上。When the common language runtime (CLR) boxes a value type, it wraps the value inside a System.Object instance and stores it on the managed heap. Unbox 處理則會從物件中擷取實值類型。Unboxing extracts the value type from the object. Boxing 是隱含處理,unboxing 則是明確處理。Boxing is implicit; unboxing is explicit. Boxing 和 unboxing 的概念是 C# 類型系統統一檢視的基礎,其中任何類型的值都可視為物件。The concept of boxing and unboxing underlies the C# unified view of the type system in which a value of any type can be treated as an object.
在下列範例中,整數變數 i
會經過 Box 處理並且指派給 o
物件。In the following example, the integer variable i
is boxed and assigned to object o
.
int i = 123;
// The following line boxes i.
object o = i;
接著就可以對物件 o
進行 Unbox 處理,並將該物件指派給整數變數 i
:The object o
can then be unboxed and assigned to integer variable i
:
o = 123;
i = (int)o; // unboxing
下列範例將說明在 C# 中使用 boxing 的方式。The following examples illustrate how boxing is used in C#.
byte[] array = { 0x64, 0x6f, 0x74, 0x63, 0x65, 0x74 };
string hexValue = Convert.ToHexString(array);
Console.WriteLine(hexValue);
/*Output:
646F74636574
*/
效能Performance
相對於單純的指派,boxing 和 unboxing 是會耗費大量運算資源的處理序。In relation to simple assignments, boxing and unboxing are computationally expensive processes. 當實值類型經過 Box 處理時,必須配置及建構新的物件。When a value type is boxed, a new object must be allocated and constructed. Unboxing 所需的轉換雖然較為簡單,但也同樣需要大量運算資源。To a lesser degree, the cast required for unboxing is also expensive computationally. 如需詳細資訊,請參閱效能。For more information, see Performance.
Box 處理Boxing
Boxing 可用來儲存記憶體回收堆積中的實值類型。Boxing is used to store value types in the garbage-collected heap. Boxing 是一種隱含轉換,可將實值型別轉換為 object
類型,或是由這個實值型別實作的任何介面類型。Boxing is an implicit conversion of a value type to the type object
or to any interface type implemented by this value type. 對實值類型進行 Boxing 處理時,會在堆積上配置物件執行個體,並將值複製到新物件中。Boxing a value type allocates an object instance on the heap and copies the value into the new object.
請考慮下列實值類型變數的宣告:Consider the following declaration of a value-type variable:
int i = 123;
下列陳述式會以隱含方式對變數 i
套用 boxing 作業:The following statement implicitly applies the boxing operation on the variable i
:
// Boxing copies the value of i into object o.
object o = i;
這個陳述式的結果是在堆疊上建立物件參考 o
,用於參考堆積中 int
類型的值。The result of this statement is creating an object reference o
, on the stack, that references a value of the type int
, on the heap. 這個值是指派給變數 i
之實值類型值的複本。This value is a copy of the value-type value assigned to the variable i
. i
和 o
這兩個變數之間的差異如下方 Boxing 轉換圖所示:The difference between the two variables, i
and o
, is illustrated in the following image of boxing conversion:
您也可以執行明確的 boxing 處理,如同下列範例中所示,但是明確的 boxing 處理並非必要:It is also possible to perform the boxing explicitly as in the following example, but explicit boxing is never required:
int i = 123;
object o = (object)i; // explicit boxing
描述Description
這個範例會使用 Boxing 將整數變數 i
轉換為物件 o
。This example converts an integer variable i
to an object o
by using boxing. 接著,儲存在變數 i
中的值就會從 123
變更為 456
。Then, the value stored in the variable i
is changed from 123
to 456
. 這個範例顯示,原始實值類型以及經過 Box 處理的物件分別使用不同的記憶體位置,因此可以儲存不同的值。The example shows that the original value type and the boxed object use separate memory locations, and therefore can store different values.
範例Example
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 doesn't affect 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 處理Unboxing
Unboxing 是將 object
類型明確轉換為實值型別,或將介面類型明確轉換為實作介面之實值型別的程序。Unboxing is an explicit conversion from the type object
to a value type or from an interface type to a value type that implements the interface. Unboxing 作業包含:An unboxing operation consists of:
檢查物件執行個體,確定它是所指定實值類型經過 Box 處理的值。Checking the object instance to make sure that it is a boxed value of the given value type.
將值從執行個體複製到實值類型變數。Copying the value from the instance into the value-type variable.
下列陳述式將示範 boxing 和 unboxing 作業:The following statements demonstrate both boxing and unboxing operations:
int i = 123; // a value type
object o = i; // boxing
int j = (int)o; // unboxing
下圖示範上述陳述式的結果:The following figure demonstrates the result of the previous statements:
若要在執行階段成功對實值類型進行 Unbox 處理,要進行 Unbox 處理的項目必須是物件的參考,而且該物件是先前對該實值類型的執行個體進行 Box 處理所建立的物件。For the unboxing of value types to succeed at run time, the item being unboxed must be a reference to an object that was previously created by boxing an instance of that value type. 嘗試對 null
進行 Unbox 處理會造成 NullReferenceException。Attempting to unbox null
causes a NullReferenceException. 嘗試對不相容的實值類型參考進行 Unbox 處理會造成 InvalidCastException。Attempting to unbox a reference to an incompatible value type causes an InvalidCastException.
範例Example
下列範例將示範 Unboxing 無效且產生 InvalidCastException
的案例。The following example demonstrates a case of invalid unboxing and the resulting InvalidCastException
. 若使用 try
和 catch
,則會在發生錯誤時顯示錯誤訊息。Using try
and catch
, an error message is displayed when the error occurs.
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);
}
}
}
這個程式會輸出:This program outputs:
Specified cast is not valid. Error: Incorrect unboxing.
如果您將陳述式:If you change the statement:
int j = (short) o;
變更為:to:
int j = (int) o;
轉換將會執行,而且您會得到下列輸出結果:the conversion will be performed, and you will get the output:
Unboxing OK.
C# 語言規格C# language specification
如需詳細資訊,請參閱 c # 語言規格。For more information, see the C# Language Specification. 語言規格是 C# 語法及用法的限定來源。The language specification is the definitive source for C# syntax and usage.