ボックス化とボックス化解除 (C# プログラミング ガイド)

ボックス化とは、値型から object 型、またはその値型によって実装されている任意のインターフェイス型へ変換するプロセスのことです。 共通言語ランタイム (CLR) により値型がボックス化されるとき、値は System.Object インスタンス内部にラップされ、マネージド ヒープに格納されます。 ボックス化解除すると、値型がオブジェクトから抽出されます。 ボックス化は暗黙的に行われ、ボックス化解除すると明示的になります。 ボックス化とボックス化解除の概念は、任意の型の値をオブジェクトとして扱うという C# の型システムの統一されたビューに基づいています。

次の例では、整数の変数 i を "ボックス化" し、オブジェクト o に代入しています。

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

次に、オブジェクト o は、次のようにボックス化解除し、整数の変数 i に代入できます。

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

次のコードは、C# でのボックス化の使用例です。

// 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

パフォーマンス

簡単な代入と比べて、ボックス化およびボックス化解除は負荷の大きいプロセスです。 値型をボックス化するときは、新しいオブジェクトを割り当てて構築する必要があります。 ボックス化ほどではありませんが、ボックス化解除に必要なキャストも大きな負荷がかかります。 詳しくは、「パフォーマンス」をご覧ください。

ボックス化

ボックス化は、値型をガベージ コレクション ヒープに格納するために使用します。 ボックス化とは、値型から object 型、またはその値型によって実装されている任意のインターフェイス型への暗黙の変換のことです。 値型をボックス化すると、オブジェクト インスタンスがヒープに割り当てられ、値が新しいオブジェクトにコピーされます。

値型の変数の宣言例を次に示します。

int i = 123;

次のステートメントは、変数 i にボックス化を暗黙的に適用します。

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

このステートメントによって、ヒープ上にある o 型の値を参照するオブジェクト参照 int がスタック上に作成されます。 この値は、変数 i に割り当てられた値型の値のコピーです。 2 つの変数 io の違いを次のボックス化変換の図に示します。

Graphic showing the difference between i and o variables.

次の例に示すように、明示的にボックス化を実行することもできますが、明示的なボックス化は不要です。

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

ここでは、ボックス化を使用して整数の変数 i をオブジェクト o に変換する例を示します。 変換後に、変数 i の値を 123 から 456 に変更します。 この例は、元の値型とボックス化されたオブジェクトが別個のメモリ位置を使用するため、それぞれ別々の値を格納できることを示しています。

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
*/

ボックス化解除

ボックス化解除とは、object 型から値型へ、またはインターフェイス型からそのインターフェイスを実装している値型への明示的な変換のことです。 ボックス化解除では、次の処理が行われます。

  • オブジェクト インスタンスが、指定された値型のボックス化された値であることを確認します。

  • インスタンスの値を値型の変数にコピーします。

次のステートメントに、ボックス化およびボックス化解除の両方を示します。

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

前のステートメントの結果は、次の図に示すとおりです。

Graphic showing an unboxing conversion.

実行時に値型のボックス化解除を成功させるには、ボックス化解除の対象項目が、同じ値型のインスタンスのボックス化によって既に作成済みのオブジェクトへの参照である必要があります。 null をボックス化解除しようとすると NullReferenceException が発生します。 互換性のない値型への参照をボックス化解除しようとすると、InvalidCastException が発生します。

次の例は、無効なボックス化解除の結果、InvalidCastException が発生する場合を示しています。 trycatch を使用すると、エラーの発生時にエラー メッセージが表示されます。

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.

C# 言語仕様

詳細については、「C# 言語の仕様」を参照してください。 言語仕様は、C# の構文と使用法に関する信頼性のある情報源です。

関連項目