Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual C#
Data Types
Boxing and Unboxing
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:

Want more? Here are some additional resources on this topic:

C# Programming Guide
Boxing and Unboxing (C# Programming Guide)

Boxing and unboxing enable value types to be treated as objects. Boxing a value type packages it inside an instance of the Object reference type. This allows the value type to be stored on the garbage collected heap. Unboxing extracts the value type from the object. In this example, the integer variable i is boxed and assigned to object o.

C#
int i = 123;
object o = (object)i;  // boxing

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

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

Performance

In relation to simple assignments, boxing and unboxing are computationally expensive processes. When a value type is boxed, an entirely 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.

Related Sections

C# Language Specification

For more information, see the following section in the C# Language Specification:

  • 4.3 Boxing and Unboxing

See Also

Community Content   What is Community Content?
Add new content RSS  Annotations
Important      G Sravan Reddy   |   Edit   |   Show History
This boxing & unboxing techniques impose a lot of performance requirements
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker