Nothing (Visual Basic)

代表任何資料型別的預設值。如果是參考型別,預設值是null的參考。實值型別的預設值而定是否可為 null 實值型別。

注意事項注意事項

不可為 null 的實值型別, Nothing Visual Basic 在不同於null C# 中。在 Visual Basic,如果您將設定為不可為 null 實值型別的變數Nothing,此變數設為預設值為其宣告的型別。在 C#,如果您指派給不可為 null 實值型別的變數null,就會發生編譯時期錯誤。

備註

Nothing表示資料型別的預設值。預設值的變數是實值型別或參考型別而定。

變數的實值型別直接包含它的值。實值型別包括所有的數字資料型別, Boolean, Char, Date,所有的結構,以及所有列舉型別。變數的參考型別物件的執行個體的參考儲存在記憶體中。參考型別包括類別、 陣列、 委派和字串。如需詳細資訊,請參閱 實值型別和參考型別

如果變數是實值鍵入的行為的Nothing變數是否為可為 null 的資料型別而定。若要顯示可為 null 的實值型別,將?的型別名稱的修飾詞。指派Nothing ,可為 null 的變數值設定為null。如需詳細資訊與範例,請參閱可為 Null 的實值型別 (Visual Basic)

如果不是可為 null 的實值型別的變數,指派Nothing到它將它設成預設值為其宣告的型別。如果該型別包含變數成員,皆會設定為自己型別的預設值。下列範例以純量型別來說明。

Module Module1

    Sub Main()
        Dim ts As TestStruct
        Dim i As Integer
        Dim b As Boolean

        ' The following statement sets ts.Name to Nothing and ts.Number to 0.
        ts = Nothing

        ' The following statements set i to 0 and b to False.
        i = Nothing
        b = Nothing

        Console.WriteLine("ts.Name: " & ts.Name)
        Console.WriteLine("ts.Number: " & ts.Number)
        Console.WriteLine("i: " & i)
        Console.WriteLine("b: " & b)

        Console.ReadKey()
    End Sub

    Public Structure TestStruct
        Public Name As String
        Public Number As Integer
    End Structure
End Module

如果變數是參考型別,指派Nothing ,此變數設為null的變數型別的參考。此變數會設定為 [ null的參考沒有任何物件相關聯。以下範例就是示範這項作業。

Module Module1

    Sub Main()

        Dim testObject As Object
        ' The following statement sets testObject so that it does not refer to
        ' any instance.
        testObject = Nothing

        Dim tc As New TestClass
        tc = Nothing
        ' The fields of tc cannot be accessed. The following statement causes 
        ' a NullReferenceException at run time. (Compare to the assignment of
        ' Nothing to structure ts in the previous example.)
        'Console.WriteLine(tc.Field1)

    End Sub

    Class TestClass
        Public Field1 As Integer
        ' . . .
    End Class
End Module

當檢查是否參考 (或型別的可為 null 值) 變數null,不會使用= Nothing或<> Nothing。Always use Is Nothing or IsNot Nothing.

在 Visual Basic 中的字串,則為空字串等於Nothing。因此, "" = Nothing為 true。

下列範例示範使用 Is 和 IsNot 運算子的比較。

Module Module1
    Sub Main()

        Dim testObject As Object
        testObject = Nothing
        Console.WriteLine(testObject Is Nothing)
        ' Output: True

        Dim tc As New TestClass
        tc = Nothing
        Console.WriteLine(tc IsNot Nothing)
        ' Output: False

        ' Declare a nullable value type.
        Dim n? As Integer
        Console.WriteLine(n Is Nothing)
        ' Output: True

        n = 4
        Console.WriteLine(n Is Nothing)
        ' Output: False

        n = Nothing
        Console.WriteLine(n IsNot Nothing)
        ' Output: False

        Console.ReadKey()
    End Sub

    Class TestClass
        Public Field1 As Integer
        Private field2 As Boolean
    End Class
End Module

如果您宣告變數而未使用As子句,並且將其設定為Nothing,則該變數的類型會是Object.其中一個範例是Dim something = Nothing。將產生編譯時期錯誤就會發生這種情況下當Option Strict上和Option Infer已關閉。

當您指派 Nothing 給物件變數時,此變數即不再參考至任一個物件執行個體。如果此變數之前已參考至某執行個體,將它設定為 Nothing 並不會終止該執行個體本身。只有在記憶體回收行程 (Garbage Collector,GC) 偵測到沒有餘留現用參考之後,執行個體才會終止,且與它相關的記憶體和系統資源會被釋放。

Nothing不同於DBNull物件,該物件代表未初始化的變數或不存在的資料庫資料行。

請參閱

參考

Dim 陳述式 (Visual Basic)

Is 運算子 (Visual Basic)

IsNot 運算子 (Visual Basic)

概念

物件存留期:物件的建立和終結 (Visual Basic)

Visual Basic 中的存留期

可為 Null 的實值型別 (Visual Basic)