1 static void Main(string[] args) 2 { 3 List<int> integers = new List<int>(); 4 for (int i = 1; i < 5; i++) 5 { 6 integers.Add(i); 7 } 8 Converter<int, double> converters = TakeSquareRoot;//创建委托实例 9 List<double> doubles; 10 doubles = integers.ConvertAll<double>(converters);//调用泛型方法转换列表 11 foreach (var item in doubles) 12 { 13 Console.WriteLine(item); 14 } 15 16 Console.ReadKey(); 17 } 18 19 public static double TakeSquareRoot(int x) 20 { 21 return Math.Sqrt(x); 22 }
时间: 2024-10-08 19:35:20