Boxing And Unboxing

Overview

  • Boxing is an implicit conversion of a value-type to the object type.
    • Boxing a value-type will copy the value on the heap, and place a pointer to the boxed value on the stack.

Good practice

  • Try to prevent boxing/unboxing when possible, use generics or multiple overloads instead.

Examples

Boxing Operation

int i = 123;
object o = (object)i;

Unboxing Operation

object o = 123;
int i = (int)o;
 
learning/exam70-536/boxing.txt · Last modified: 2008/08/26 16:05 by david
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki