Nothing 和字符串 (Visual Basic)

Visual Basic 运行时和 .NET Framework 在处理字符串时,对于 Nothing 的评估方式有所不同。

Visual Basic 运行时和 .NET Framework

请看下面的示例:

Dim MyString As String = "This is my string"
Dim stringLength As Integer

' Explicitly set the string to Nothing.
MyString = Nothing

' stringLength = 0
stringLength = Len(MyString)

' This line, however, causes an exception to be thrown.
stringLength = MyString.Length

Visual Basic 运行时通常将 Nothing 评估为空字符串 ("")。 而 .NET Framework 不会这样求值,只要尝试对 Nothing 执行字符串操作,它就会引发异常。

另请参阅