Being in the programming game for so long, you tend to forget the basic principles of the .NET language. Today's topic is no exception. For experienced developers, a topic such as boxing and unboxing might already be second nature, but for the inexperienced programmer, this topic is vital, and the sooner you learn it, the better.
Now, let me get this straight. This is not Floyd Mayweather versus Manny Pacquiao. I just had to get that out of the way....
This means that, once a variable is boxed, it can hold a separate value in a separate memory location. When a value type is boxed, a new object must be allocated and constructed.
A simple example of Boxing looks like this:
Dim i As Integer i = 123 Dim o As Objecto = i
The value of i gets copied into the variable o, because these two value types are compatible. Another example of Boxing is:
Dim i As Integer i = 123 Dim L As Long L = i
The variable i is compatible with the Long object, so it could be easily copied. For more information on Value types, please read the following:
For more information regarding the Garbage Collector and Garbage Collection, please read the following:
Unboxing simply extracts the value type from the object or the interface type. In contrast to boxing, the cast required for unboxing is not as expensive computationally. Unboxing is a conversion from the type Object to a value type or from an interface type to a value type that implements the interface.
An unboxing operation consists of the following:
- Determining if the value to be unboxed is a boxed value.
- Copying the value from the instance into the value type variable.
A simple example of Unboxing is as follows:
Dim i As Integer Dim o As Object o = 123 i = DirectCast(o, i)
Here, the numeric value is initially of Type Object; then, it gets converted to an Integer value. For more information on Reference types, have a read through here:
For more information regarding all the .NET Framework types, have a look through these:
Let's do a proper example. Open Visual Basic and create a new Console application.
Declare the following variables:
' Integer Dim _intvalue As Integer = 15 ' object type Dim _Obj As [Object] = 200
intValue gets created with the value of 15 and as an Integer value type. Obj gets created as an Object type with the value of 200 stored inside it.
Add the following code to Box a variable:
' Boxing _Obj = DirectCast(_intvalue, [Object]) System.Console.WriteLine(_Obj) System.Console.Read()
Once run this, you will see the value 15 displayed. Why? Well, because the Obj object now contains the boxed value of the intValue variable.
Add the following code to Unbox the value:
' UnBoxing _intvalue = CInt(_Obj) System.Console.WriteLine(_intvalue) System.Console.Read()
The Obj value gets unboxed back into intValue and now again holds the value of 15. Your full code looks like the following:
Imports System Imports System.Collections.Generic Imports System.Text Module Module1 Sub Main() ' Boxing ' Integer Dim _intvalue As Integer = 15 ' object type Dim _Obj As [Object] = 200 ' Boxing _Obj = DirectCast(_intvalue, [Object]) System.Console.WriteLine(_Obj) System.Console.Read() ' UnBoxing _intvalue = CInt(_Obj) System.Console.WriteLine(_intvalue) System.Console.Read() End Sub End Module
When run, your screen will look like Figure 1:
Figure 1: The results of our program
This working sample is included with this article.
Short, but to the point. I. Hope you have enjoyed today's article. Until next time, cheers!