ARTICLE

Boxing and Unboxing

Posted by Puran Mehra Articles | Visual Basic 2010 August 25, 2009
Tags: Boxing, Unboxing
In this article I will explain you about Boxing and Unboxing in VB.NET.
 
Reader Level:

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.

Your feedback and constructive contributions are welcome. Please feel free to contact me for feedback or comments you may have about this article.

Login to add your contents and source code to this article
share this article :
post comment
 

thanks for correcting.

Posted by Puran Mehra Sep 08, 2009

BOXING AND UNBOXING

Is unboxing portion of your code correct? 
I think you mean " j=CInt(o) ".

Posted by Prasanth Kumar Sep 07, 2009
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor