Boxing and Unboxing
The common type system enables implicit conversions. It also enables conversions between reference types and value types—and vice versa—because both inherit from System.Object
. You often need to work with two techniques, boxing and unboxing, when you have methods that receive arguments of type Object
.
Boxing
Boxing occurs when you convert a value type to a reference type. In other words, boxing happens when you assign a value type to an Object
. The following lines of code demonstrate boxing:
Dim calculation As Double = 14.4 + 32.12Dim result As Object = calculation
In this example, the calculation
variable, which stores a value deriving from the sum of two numbers, is a Double
value type. The ...
Get Visual Basic 2015 Unleashed now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.