Array.ConvertAll数组间转换
Array.ConvertAll数组间转换一、Array.ConvertAll语法
public static TOutput[] ConvertAll<TInput,TOutput> (
TInput[] array,
Converter<TInput,TOutput> converter
)
通俗理解
目标数组[] 目标数组类型 = Array.ConvertAll ( 源数组名,new Converter<源数组类型,目标数组类型>(自定义的转换方法))
二、Array.ConvertAll实例
public class Example
{
static void Main()
{
int [] int_array = { 1, 2, 3 };
string [] str_array = Array.ConvertAll(int_array, new Converter< int , string >(IntToString));
foreach ( string s in str_array)
{
Console.WriteLine(s);
}
Console.Read();
}
public static string IntToString( int i)
{
return i.ToString();
}
}
实例2:用linq实现
string[] ids = new string[5] { "5", "6", "7", "8", "9" };
int[] ids2 = Array.ConvertAll(ids, id => Convert.ToInt32(id));