Boxing 및 Unboxing(C# 프로그래밍 가이드)

Boxing은 값 형식object 형식 또는 이 값 형식에서 구현된 임의의 인터페이스 형식으로 변환하는 프로세스입니다. CLR(공용 언어 런타임)은 값 형식을 boxing할 때 값을 System.Object 인스턴스 내부에 래핑하고 관리되는 힙에 저장합니다. unboxing하면 개체에서 값 형식이 추출됩니다. Boxing은 암시적이며 unboxing은 명시적입니다. Boxing 및 unboxing의 개념은 개체로 처리할 수 있는 모든 값 형식에서 형식 시스템의 C#에 통합된 뷰의 기반이 됩니다.

다음 예제에서는 정수 변수 iboxing하고 개체 o에 할당합니다.

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

그런 다음 o 개체를 unboxing하고 정수 변수 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

성능

단순 할당에서는 boxing과 unboxing을 수행하는 데 많은 계산 과정이 필요합니다. 값 형식을 boxing할 때는 새로운 개체를 할당하고 생성해야 합니다. 정도는 약간 덜하지만 unboxing에 필요한 캐스트에도 상당한 계산 과정이 필요합니다. 자세한 내용은 성능을 참조하세요.

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에 할당된 값 형식 값의 복사본입니다. 두 변수 io의 차이점은 boxing 변환을 보여주는 다음 이미지에 나와 있습니다.

Graphic showing the difference between i and o variables.

다음 예제에서와 같이 명시적으로 boxing을 수행할 수도 있지만 명시적 boxing이 반드시 필요한 것은 아닙니다.

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

예시

이 예제에서는 boxing을 통해 정수 변수 i를 개체 o로 변환합니다. 그런 다음 변수 i에 저장된 값을 123에서 456으로 변경합니다. 이 예제에서는 원래 값 형식과 boxing된 개체에 개별 메모리 위치를 사용하여 서로 다른 값을 저장하는 방법을 보여 줍니다.

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

unboxing

Unboxing은 object 형식에서 값 형식으로, 또는 인터페이스 형식에서 해당 인터페이스를 구현하는 값 형식으로 명시적으로 변환하는 프로세스입니다. unboxing 연산 과정은 다음과 같습니다.

  • 개체 인스턴스가 지정한 값 형식을 boxing한 값인지 확인합니다.

  • 인스턴스의 값을 값 형식 변수에 복사합니다.

다음 문은 boxing 및 unboxing 연산을 모두 보여 줍니다.

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

다음 그림에서는 이전 문의 결과를 보여 줍니다.

Graphic showing an unboxing conversion.

런타임에 값 형식의 unboxing이 성공하려면 unboxing되는 항목은 이전에 해당 값 형식의 인스턴스를 boxing하여 생성된 개체에 대한 참조여야 합니다. null을 unboxing하려고 하면 NullReferenceException이 발생합니다. 호환되지 않는 값 형식에 대한 참조를 unboxing하려고 하면 InvalidCastException이 발생합니다.

예시

다음 예제에서는 잘못된 unboxing의 경우와 그 결과로 발생하는 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# 구문 및 사용법에 대 한 신뢰할 수 있는 소스 됩니다.

참고 항목