What is the best way to clear an array of strings?
Use List. clear() method to empty an array.
To empty an array in C#, use the Array Clear() method: The Array. Clear method in C# clears i.e.zeros out all elements.
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)
And of course there's the VB way using the Erase keyword:
Dim arr() as String = {"a","b","c"}
Erase arr
Depending what you want:
Last is likely to be slowest, but only option if you don't want a new array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With