VB.Net Tutorial/Data Type/Boxing unboxing

Материал из VB Эксперт
Перейти к: навигация, поиск

Boxing and unboxing Integer

<source lang="vbnet">Option Strict On

Imports System
Public Class UnboxingTest
    Public Shared Sub Main( )
        Dim myIntegerVariable As Integer = 123
        " Boxing
        Dim myObjectVariable As Object = myIntegerVariable
        Console.WriteLine("myObjectVariable: {0}",myObjectVariable.ToString( ))
        " unboxing (must be explicit)
        Dim anotherIntegerVariable As Integer = DirectCast(myObjectVariable, Integer)
        Console.WriteLine("anotherIntegerVariable: {0}",anotherIntegerVariable)
    End Sub
End Class</source>
myObjectVariable: 123
anotherIntegerVariable: 123