Nothing 關鍵字 (Visual Basic)

表示任何資料類型的預設值。 針對參考型別,預設值為 null 參考。 針對實值型別,預設值取決於實值型別是否可為 Null。

注意

針對不可為 Null 的實值型別,Visual Basic 中的 Nothing 與 C# 中的 null 不同。 在 Visual Basic 中,若您將不可為 Null 實值型別的變數設定為 Nothing,則變數會設定為其宣告類型的預設值。 在 C# 中,若您將不可為 Null 實值型別的變數指派給 null,就會發生編譯時間錯誤。

備註

Nothing 表示任何資料類型的預設值。 預設值取決於變數是實值型別還是參考型別。

「實值型別」的變數直接包含其值。 實值型別包括所有數值資料類型、BooleanCharDate、所有結構,以及所有列舉。 「參考型別」的變數會將物件的參考儲存於記憶體中。 參考型別包括類別、陣列、委派與字串。 如需詳細資訊,請參閱 Value Types and Reference Types

若變數是實值型別,則 Nothing 的行為取決於變數是否為可為 Null 的資料類型。 若要表示可為 Null 的實值型別,請將 ? 修飾詞新增至類型名稱。 將 Nothing 指派給可為 Null 的變數會將值設定為 null。 如需詳細資訊與範例,請參閱可為 Null 的實值型別

若變數的實值型別不可為 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 null 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 時,請一律使用 Is NothingIsNot Nothing。 不要使用此 = Nothing<> Nothing

針對 Visual Basic 中的字串,空白字串等於 Nothing。 因此,"" = Nothing 為 true。 這一事實使得在處理字串時選擇正確的比較尤為重要。 雖然 myString = NothingmyString <> Nothing 指出是否已設定非空值,但我們強烈建議針對此目的使用 String.IsNullOrEmpty(myString)。 使用 Is NothingIsNot Nothing 來判斷是否已設定任何值,包括空字串。

下列範例顯示使用 IsIsNot 運算子的比較:

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 不會終止執行個體本身。 只有在記憶體回收行程 (GC) 偵測到沒有剩餘的使用中參考之後,執行個體才會終止,並釋放與其建立關聯的記憶體與系統資源。

NothingDBNull 物件不同,其表示未初始化的變體或不存在的資料庫資料行。

另請參閱