Boxing and Unboxing in VB.NET

In this article I will explain you about Boxing and Unboxing in VB.NET.
  • 8305

Boxing and unboxing is an important concept in VB.NET's type system. With Boxing and Unboxing one can link between value-types and reference-types by allowing any value of a value-type to be converted to and from type object.

Boxing

  • Boxing is a mechanism in which value type is converted into reference type.
  • It is implicit conversion process in which object type (super type) is used.
  • In this process type and value both are stored in object type
Unboxing
  • Unboxing is a mechanism in which reference type is converted into value.
  • It is explicit conversion process.
Program to show Boxing and Unboxing:

Module
Module1
    Sub Main()
        Dim i As Integer = 10
        Dim j As Integer
        ' boxing
        Dim o As Object
        o = i
        'unboxing
        j = CInt(o)
        Console.WriteLine("value of o object : " & o)
        Console.WriteLine("Value of j : " & j)
        Console.ReadLine()
    End Sub

End
Module

Output of above program:

boxunox.gif

Conclusion:

Hope the article might have helped you in understanding Boxing and Unboxing.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.