我需要一种简单的方法将a转换List<int>为string数组.
我有:
var the_list = new List<int>();
the_list.Add(1);
the_list.Add(2);
the_list.Add(3);
string[] the_array = new string[the_list.Count];
for(var i = 0 ; i < the_array.Count; ++i)
the_array[i] = the_list[i].ToString();
Run Code Online (Sandbox Code Playgroud)
......对我来说这看起来很难看.
有没有更简单的方法?
注意:我正在寻找一种更简单的方法 - 不一定是更快的方式.
Eti*_*tel 57
使用LINQ:
string[] the_array = the_list.Select(i => i.ToString()).ToArray();
Run Code Online (Sandbox Code Playgroud)
我知道您有一个很好的答案,但您不需要 LINQ 或 Select。您可以使用 ConvertAll 和匿名方法来完成。像这样:
var list = new List<int>();
....
var array = list.ConvertAll( x => x.ToString() ).ToArray();
Run Code Online (Sandbox Code Playgroud)
类似的想法,但我认为这不是 linq。以防万一。
| 归档时间: |
|
| 查看次数: |
38161 次 |
| 最近记录: |