Nothing (Visual Basic)Nothing (Visual Basic)

表示任何資料類型的預設值。Represents the default value of any data type. 對於參考型別, 預設值為null參考。For reference types, the default value is the null reference. 對於實數值型別, 預設值取決於實數值型別是否可為 null。For value types, the default value depends on whether the value type is nullable.

注意

針對不可為 null 的實值Nothing類型, 在中null , C#Visual Basic 與中的不同。For non-nullable value types, Nothing in Visual Basic differs from null in C#. 在 Visual Basic 中, 如果您將不可為 null 的實數值型別變數設Nothing為, 則變數會設定為其宣告類型的預設值。In Visual Basic, if you set a variable of a non-nullable value type to Nothing, the variable is set to the default value for its declared type. 在C#中, 如果您將不可為 null 的實數值型別的變數null指派給, 就會發生編譯時期錯誤。In C#, if you assign a variable of a non-nullable value type to null, a compile-time error occurs.

備註Remarks

Nothing表示資料類型的預設值。Nothing represents the default value of a data type. 預設值取決於變數是實數值型別或參考型別。The default value depends on whether the variable is of a value type or of a reference type.

值型別的變數直接包含它的值。A variable of a value type directly contains its value. 實數值型別包括所有數值資料類型BooleanCharDate、、所有結構和所有列舉。Value types include all numeric data types, Boolean, Char, Date, all structures, and all enumerations. 參考型別的變數會將物件實例的參考儲存在記憶體中。A variable of a reference type stores a reference to an instance of the object in memory. 參考型別包括類別、陣列、委派和字串。Reference types include classes, arrays, delegates, and strings. 如需詳細資訊,請參閱 Value Types and Reference TypesFor more information, see Value Types and Reference Types.

如果變數是實值型別, 則的行為Nothing取決於變數是否為可為 null 的資料型別。If a variable is of a value type, the behavior of Nothing depends on whether the variable is of a nullable data type. 若要表示可為 null 的實值?型別, 請將修飾詞加入至型別名稱。To represent a nullable value type, add a ? modifier to the type name. 指派Nothing給可為 null 的變數會將null值設定為。Assigning Nothing to a nullable variable sets the value to null. 如需詳細資訊和範例, 請參閱可為 null 的實數值型別For more information and examples, see Nullable Value Types.

如果變數是不可為 null 的實數值型別, 則指派Nothing給它會將它設定為其宣告類型的預設值。If a variable is of a value type that is not nullable, assigning Nothing to it sets it to the default value for its declared type. 如果該類型包含變數成員, 則會將它們全部設定為預設值。If that type contains variable members, they are all set to their default values. 下列範例會針對純量類型說明這種情況。The following example illustrates this for scalar types.

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為變數類型的參考。If a variable is of a reference type, assigning Nothing to the variable sets it to a null reference of the variable's type. 設定為null參考的變數不會與任何物件相關聯。A variable that is set to a null reference is not associated with any object. 下列範例為其示範。The following example demonstrates this.

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不要使用或。When checking whether a reference (or nullable value type) variable is null, do not use = Nothing or <> Nothing. 請一律Is Nothing使用IsNot Nothing或。Always use Is Nothing or IsNot Nothing.

對於 Visual Basic 中的字串, 空字串等於NothingFor strings in Visual Basic, the empty string equals Nothing. 因此, "" = Nothing為 true。Therefore, "" = Nothing is true.

下列範例顯示使用IsIsNot運算子的比較。The following example shows comparisons that use the Is and IsNot operators.

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變數的類型為。If you declare a variable without using an As clause and set it to Nothing, the variable has a type of Object. 其中一個範例是Dim something = NothingAn example of this is Dim something = Nothing. 當是 on 且為 off 時Option Strict , Option Infer就會發生編譯時期錯誤。A compile-time error occurs in this case when Option Strict is on and Option Infer is off.

當您指派Nothing給物件變數時, 它不會再參考任何物件實例。When you assign Nothing to an object variable, it no longer refers to any object instance. 如果變數先前已參考實例, 將其設定為Nothing不會終止實例本身。If the variable had previously referred to an instance, setting it to Nothing does not terminate the instance itself. 實例會終止, 而且只有在垃圾收集行程 (GC) 偵測到沒有任何使用中的參考之後, 才會釋放與它相關聯的記憶體和系統資源。The instance is terminated, and the memory and system resources associated with it are released, only after the garbage collector (GC) detects that there are no active references remaining.

Nothing不同于DBNull物件, 代表未初始化的 variant 或不存在的資料庫資料行。Nothing differs from the DBNull object, which represents an uninitialized variant or a nonexistent database column.

另請參閱See also