Boxing은 값 형식을 object 형식 또는 값 형식에서 구현된 임의의 인터페이스 형식으로 변환하는 프로세스입니다.
CLR(공용 언어 런타임)은 값 형식을 boxing할 때 값을 System.Object 인스턴스 내부에 래핑하고 관리되는 힙에 저장합니다.
unboxing을 하면 개체에서 값 형식이 추출됩니다.
boxing은 암시적이며 unboxing은 명시적입니다.
// 정수 변수 i를 boxing하고 개체를 o에 할당합니다.
int i = 123;
// The following line boxes i.
object o = i;
// o개체를 unboxing하고 정수 변수 i에 할당할수 있습니다.
o = 123;
i = (int)o; // unboxing
// 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;
// Boxing copies the value of i into object o.
object o = i;
이 문의 결과로 힙에 있는 o형식의 값을 참조하는 int 개체 참조가 스택에 생성됩니다.
이 값은 변수 i에 할당된 값 형식 값의 복사본입니다.
두 변수 i 및 o의 차이점 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 연산 과정은 아래와 같습니다.
1. 개체 인스턴스가 지정된 값 형식을 boxing한 값인지 확인합니다.
2. 인스턴스의 값을 값형식 변수에 복사합니다.
int i = 123; // a value type
object o = i; // boxing
int j = (int)o; // unboxing
런타임에 값 형식의 unboxing이 성공하려면 unboxing되는 항목은 이전에 해당 값 형식의 인스턴스를 boxing하여 생성된 개체에 대한 참조여야 합니다.
null을 unboxing하려고 하면 NullReferenceException이 발생합니다.
호환되지 않는 값 형식에 대한 참조를 unboxing하려고 하면 InvaildCastException이 발생합니다.
아래 예제에서는 잘못된 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("{0} Error: Incorrect unboxing.", e.Message);
}
}
}
// output
// Specified cast is not valid. Error: Incorrect unboxing.
class TestUnboxing
{
static void Main()
{
int i = 123;
object o = i; // implicit boxing
try
{
int j = (int)o; // attempt to unbox
System.Console.WriteLine("Unboxing OK.");
}
catch (System.InvalidCastException e)
{
System.Console.WriteLine("{0} Error: Incorrect unboxing.", e.Message);
}
}
}
// output
// Unboxing OK.
'C#' 카테고리의 다른 글
C# Value Type : 값 형식 - 정수 숫자 형식 (0) | 2023.01.12 |
---|---|
C# Value Type : 값 형식 (0) | 2023.01.12 |
C# Type Conversion : 형변환 (0) | 2023.01.10 |
C# Memory : 메모리 (0) | 2023.01.10 |
C# 병렬화와 비동기 패턴 (0) | 2023.01.09 |