Since arrays are reference types, an array variable can be null. To declare a null array variable, you must declare it without a size:
Dim array() As Integer
Or
Dim array As Integer()
To check if an array is null, test to see if it Is Nothing
:
Dim array() As Integer
If array Is Nothing Then
array = {1, 2, 3}
End If
To set an existing array variable to null, simply set it to Nothing
:
Dim array() As Integer = {1, 2, 3}
array = Nothing
Console.WriteLine(array(0)) ' Throws a NullReferenceException
Or use Erase
, which does the same thing:
Dim array() As Integer = {1, 2, 3}
Erase array
Console.WriteLine(array(0)) ' Throws a NullReferenceException