What is 'boxing'?
Assigning values within objects, to be used as a reference, (making value, types reference types), or also known as type-conversions, boxing is a key conversion process involving changes/edits from a value type to the type object or to any interface type implemented or associated by this value type. When 'boxing' a value type, the value is wrapped inside a System.Object stored on the managed heap. In order to extract it again, unboxing will take the value type from the object again.(see unboxing elsewhere).
These are very typically used for simple/non-complex tasks and assignments. They are resource-intensive processes, new allocations and constucts take space and capacity and can impact performance.
This is a storage tactic for value types in the garbage collected heap as well as an extremely useful way to get additional functionality, but it is taxing on system resources and memory, so care has to be taken with it. Boxed value types can then perhaps be modified in some cases where distribution of a copy of the type that cannot modify the original has been done. Boxing and Unboxing Type Conversions, should not be done for intensive, repetitive tasks. Utilization of type specific versionsare preferred for those processes requiring a variety of value types (also see elsewhere: overloads and overrides). The use of generics are preferred over codings conversions using object type - conversion operators and enablers make this possible and have to be specified.
Key Exam Points
- Uses, applications, scenarios and solutions where Boxing/Unboxing is best used, how and why, the implications of the utility and tool - review the relevant chapters, content and references in detail, (both theory/paramaters and applications).
- For the exam, study examples for scenarios, rules, protocol, code and application of the concept , that help you to gain a better understanding of this topic
- Take the Practice Exam, work-lab exercises, Q&A (with answers and solutions).
Type Conversion, Unboxing, Narrowing, Widening
This article is based on the 2nd edition of the Microsoft .Net Framework Application Training Kit with the purpose to help 70-536 Exam takers to succeed. I constantly look for ways to improve the content. Please leave a comment about this article or drop me a message if you would like to see changes for this site.