What is the best way to clear an array of strings?

Wrong way:

myArray = Nothing 

Only sets the variable pointing to the array to nothing, but doesn't actually clear the array. Any other variables pointing to the same array will still hold the value. Therefore it is necessary to clear out the array.

Correct Way

Array.Clear(myArray,0,myArray.Length)

Depending what you want:

  • Assign Nothing (null)
  • Assign a new (empty) array
  • Array.Clear

Last is likely to be slowest, but only option if you don't want a new array.


And of course there's the VB way using the Erase keyword:

Dim arr() as String = {"a","b","c"}
Erase arr

Tags:

Vb.Net

Arrays