Επεξεργασία

Κοινή χρήση μέσω


Nothing keyword (Visual Basic)

Represents the default value of any data type. For reference types, the default value is the null reference. For value types, the default value depends on whether the value type is nullable.

Note

For non-nullable value types, Nothing in Visual Basic differs from null in C#. 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. In C#, if you assign a variable of a non-nullable value type to null, a compile-time error occurs.

Remarks

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. 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. For more information, see Value Types and Reference Types.

If a variable is of a value type, the behavior of Nothing depends on whether the variable is of a nullable data type. To represent a nullable value type, add a ? modifier to the type name. Assigning Nothing to a nullable variable sets the value to null. For more information and examples, see Nullable Value Types.

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 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

If a variable is of a reference type, assigning Nothing to the variable sets it to a null reference of the variable's type. 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

To check whether a reference (or nullable value type) variable is null, always use Is Nothing or IsNot Nothing. Don't use = Nothing or <> Nothing.

For strings in Visual Basic, the empty string equals Nothing. Therefore, "" = Nothing is true. This fact makes it especially important that you choose the correct comparison when you work with strings. Although myString = Nothing and myString <> Nothing indicate whether a non-empty value is set, we strongly recommend using String.IsNullOrEmpty(myString) for this purpose. Use Is Nothing and IsNot Nothing to determine whether any value, including an empty string, was set.

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

If you declare a variable without using an As clause and set it to Nothing, the variable has a type of Object. An example of this is Dim something = Nothing. A compile-time error occurs in this case when Option Strict is on and Option Infer is off.

When you assign Nothing to an object variable, it no longer refers to any object instance. If the variable had previously referred to an instance, setting it to Nothing does not terminate the instance itself. 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 differs from the DBNull object, which represents an uninitialized variant or a nonexistent database column.

See also