清除字符串数组

use*_*651 11 c# string

清除字符串数组的最简单方法是什么?

Jon*_*eet 33

你试过Array.Clear吗?

string[] foo = ...;
Array.Clear(foo, 0, foo.Length);
Run Code Online (Sandbox Code Playgroud)

请注意,这不会改变数组的大小 - 没有什么能做到这一点.相反,它会将每个元素设置为null.

如果您需要可以实际改变大小的东西,请使用List<string>:

List<string> names = new List<string> { "Jon", "Holly", "Tom" };
names.Clear(); // After this, names will be genuinely empty (Count==0)
Run Code Online (Sandbox Code Playgroud)


Guf*_*ffa 9

Array.Clear(theArray, 0, theArray.Length);
Run Code Online (Sandbox Code Playgroud)