Lecture overview -- Keyboard shortcut: 'u'  Previous page: Examples of immutable structs in C# -- Keyboard shortcut: 'p'  Next page: Nullable types -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Help page about these notes  Alphabetic index  Course home  Page 15 : 29
Object-oriented Programming in C#
Reference types, Value types, and Patterns
Boxing and Unboxing

Boxing is a bridge from value types to reference types.

The use of boxing facilitates compatibility between value types and reference types

Boxing involves a wrapping of a value in an object of the class Object

Unboxing involves an unwrapping of a value from an object

  • Boxing

    • Done as an implicit type conversion

    • Involves allocation of a new object on the heap and copying of the value into that object

  • Unboxing

    • Done explicitly via a type cast

    • Involves extraction of the value of a box

/user/normark/oop-csharp-1/sources/c-sharp/boxing/boxing.csA program that illustrates boxing and unboxing.

/user/normark/oop-csharp-1/sources/c-sharp/boxing/outputOutput of the boxing and unboxing program.