またConvertAllメソッドは、型変換だけでなくすべての要素に同じ処理を施した結果を取得するといった方法にも使えます。
ConvertAllメソッドを使ってList内のすべての文字列を長さを求めたListを作成する C# 2.0
using System;
using System.Collections.Generic;
class Sample {
static void Main()
{
var stringList = new List<string>() {"Alice", "Bob", "Charlie", "Dave", "Eve"};
// stringListのすべての要素に対して、その長さを求めたListを作成する
var lengthList = stringList.ConvertAll(delegate(string s) { return s.Length; });
for (var index = 0; index < stringList.Count; index++) {
Console.WriteLine("\"{0}\".Length = {1}", stringList[index], lengthList[index]);
}
}
}
実行結果
"Alice".Length = 5 "Bob".Length = 3 "Charlie".Length = 7 "Dave".Length = 4 "Eve".Length = 3