Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

arrays

vb.net

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

like image 519
spacemonkeys Avatar asked Apr 03 '09 13:04

spacemonkeys


People also ask

How do you clear an array of strings in Java?

Use List. clear() method to empty an array.

How do you clear an array of strings in C#?

To empty an array in C#, use the Array Clear() method: The Array. Clear method in C# clears i.e.zeros out all elements.


3 Answers

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)
like image 174
Micah Avatar answered Nov 02 '22 16:11

Micah


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

Dim arr() as String = {"a","b","c"}
Erase arr
like image 36
Dustin Campbell Avatar answered Nov 02 '22 16:11

Dustin Campbell


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.

like image 33
Richard Avatar answered Nov 02 '22 17:11

Richard