Permalink
Fetching contributors…
Cannot retrieve contributors at this time
144 lines (96 sloc) 6.46 KB
title ms.date ms.prod ms.technology ms.topic f1_keywords dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author translation.priority.ht translation.priority.mt
Boxing and Unboxing (C# Programming Guide)
2015-07-20
.net
devlang-csharp
article
cs.boxing
CSharp
C# language, boxing
C# language, unboxing
unboxing [C#]
boxing [C#]
8da9bbf4-bce9-4b08-b2e5-f64c11c56514
34
BillWagner
wiwagn
de-de
es-es
fr-fr
it-it
ja-jp
ko-kr
ru-ru
zh-cn
zh-tw
cs-cz
pl-pl
pt-br
tr-tr

Boxing and Unboxing (C# Programming Guide)

Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap. Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit. 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.

In the following example, the integer variable i is boxed and assigned to object o.

[!code-cscsProgGuideTypes#14]

The object o can then be unboxed and assigned to integer variable i:

[!code-cscsProgGuideTypes#15]

The following examples illustrate how boxing is used in C#.

[!code-cscsProgGuideTypes#47]

Performance

In relation to simple assignments, boxing and unboxing are computationally expensive processes. When a value type is boxed, a new object must be allocated and constructed. To a lesser degree, the cast required for unboxing is also expensive computationally. For more information, see Performance.

Boxing

Boxing is used to store value types in the garbage-collected heap. Boxing is an implicit conversion of a value type to the type object or to any interface type implemented by this value type. 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:

[!code-cscsProgGuideTypes#17]

The following statement implicitly applies the boxing operation on the variable i:

[!code-cscsProgGuideTypes#18]

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. This value is a copy of the value-type value assigned to the variable i. The difference between the two variables, i and o, is illustrated in the following figure.

BoxingConversion graphic
Boxing Conversion

It is also possible to perform the boxing explicitly as in the following example, but explicit boxing is never required:

[!code-cscsProgGuideTypes#19]

Description

This example converts an integer variable i to an object o by using boxing. Then, the value stored in the variable i is changed from 123 to 456. The example shows that the original value type and the boxed object use separate memory locations, and therefore can store different values.

Example

[!code-cscsProgGuideTypes#16]

Unboxing

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. An unboxing operation consists of:

  • 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.

The following statements demonstrate both boxing and unboxing operations:

[!code-cscsProgGuideTypes#21]

The following figure demonstrates the result of the previous statements.

UnBoxing Conversion graphic
Unboxing Conversion

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. Attempting to unbox null causes a xref:System.NullReferenceException. Attempting to unbox a reference to an incompatible value type causes an xref:System.InvalidCastException.

Example

The following example demonstrates a case of invalid unboxing and the resulting InvalidCastException. Using try and catch, an error message is displayed when the error occurs.

[!code-cscsProgGuideTypes#20]

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# Language Specification

[!INCLUDECSharplangspec]

Related Sections

For more information:

C# Language Specification

[!INCLUDECSharplangspec]

See Also

C# Programming Guide