VB.NET缺少isNull()?

Tom*_*Tom 5 vb.net

我想确认数组是否已创建,如何才能完成?没有nul关键字?

Dim items As Array = str.Split("|")
if (items=null)  then ???
Run Code Online (Sandbox Code Playgroud)

Mat*_*ser 24

要检查VB.Net中的对象是否为null,您需要使用Nothing关键字.例如

If (items is Nothing) Then
    'do stuff
End If
Run Code Online (Sandbox Code Playgroud)

但是string.Split()永远不会返回null,因此您应该检查输入字符串是否为null而不是items数组.您的代码可以更改为:

If Not String.IsNullOrEmpty(str) Then
    Dim items As Array = str.Split("|")
    'do stuff
End If
Run Code Online (Sandbox Code Playgroud)


Kev*_*evB 7

String.IsNullOrEmpty在分割之前尝试使用字符串变量.如果您尝试在字符串中没有任何内容的情况下拆分变量,则数组中仍会有一个项目(空字符串),因此IsNothing对数组的检查将返回false.