Nothing 关键字 (Visual Basic)

表示任何数据类型的默认值。 对于引用类型,默认值为 null 引用。 对于值类型,默认值取决于值类型是否可为 null。

注释

对于不可为 null 的值类型, Nothing 在 Visual Basic 中与 null C# 不同。 在 Visual Basic 中,如果将不可为 null 的值类型的 Nothing变量设置为,则该变量将设置为其声明类型的默认值。 在 C# 中,如果将不可为 null 的值类型的变量赋给 null,则会发生编译时错误。

注解

Nothing 表示数据类型的默认值。 默认值取决于变量是值类型还是引用类型。

值类型的变量直接包含其值。 值类型包括所有数值数据类型、 BooleanCharDate所有结构和所有枚举。 引用类型的变量将对象实例的引用存储在内存中。 引用类型包括类、数组、委托和字符串。 有关详细信息,请参阅 值类型和引用类型

如果变量是值类型,则 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 属实。 这一事实使得在处理字符串时选择正确的比较尤其重要。 虽然 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

如果在不使用子句的情况下 声明变量并将其设置为, 则变量的类型为 < a0/>。 下面是一 Dim something = Nothing个示例。 如果处于打开状态且Option Infer处于关闭状态,则会出现Option Strict编译时错误。

分配给 Nothing 对象变量时,它不再引用任何对象实例。 如果变量以前引用了实例,则将其设置为 Nothing 不终止实例本身。 实例已终止,并且仅当垃圾回收器(GC)检测到没有活动引用剩余后,才会释放与其关联的内存和系统资源。

Nothing 不同于对象 DBNull ,该对象表示未初始化的变体或不存在的数据库列。

另请参阅