Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear array of strings

Tags:

string

c#

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

like image 839
user404651 Avatar asked Sep 26 '10 12:09

user404651


1 Answers

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)
like image 107
Jon Skeet Avatar answered Oct 22 '22 06:10

Jon Skeet