Monday, 9 October 2017

Boxing and Unboxing in VB.Net


Boxing and Unboxing in VB.Net:


VB provides us with Value types and Reference Types. Value Types are stored on the stack and Reference types are stored on the heap. The conversion of value type to reference type is known as Boxing and converting reference type back to the value type is known as Unboxing


Boxing:

Convert ValueTypes to Reference Types also known as boxing.

Dim x As Int32 = 10
Dim o AsObject = x ' Implicit boxing
Console.WriteLine("The Object o = ", &o) ' prints out 10

Dim x As Int32 = 10
Dim o AsObject = CObj(x) ' Explicit Boxing
Console.WriteLine("The object o = ", &o) ' prints out 10

Unboxing:

UnBoxing an object type back to value type.
Dim x As Int32 = 5
Dim o As Object = x ' Implicit Boxing
x = o ' Implicit UnBoxing

Dim x As Int32 = 5
Dim o As Object = x ' Implicit Boxing
x = CInt(Fix(o)) ' Explicit UnBoxing

0 comments

Post a Comment