tags:

views:

103

answers:

3

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

+3  A: 
Array.Clear(theArray, 0, theArray.Length);
Guffa
That won't compile - Clear isn't an instance method, and it isn't parameterless either.
Jon Skeet
@Jon Skeet: Too late, I already updated it. ;)
Guffa
+8  A: 

Have you tried Array.Clear?

string[] foo = ...;
Array.Clear(foo, 0, foo.Length);

Note that this won't change the size of the array - nothing will do that. Instead, it will set each element to null.

If you need something which can actually change size, use a List<string> instead:

List<string> names = new List<string> { "Jon", "Holly", "Tom" };
names.Clear(); // After this, names will be genuinely empty (Count==0)
Jon Skeet
Thanks! I couldn't understand why I couldn't find clear under string methods. The answer, of course, is that it's an array method.
+1  A: 

use array's Clear() method:

bx2