Exists・TrueForAllメソッドを使って条件に合致する要素がListに含まれるかどうか調べる(ラムダ式版) C# 3.0
using System;
using System.Collections.Generic;
class Sample {
static void Main()
{
var list = new List<string>() {"Alice", "Bob", "Charlie", "Dave", "Eve"};
// 長さが5以上の要素が存在するかどうか
Console.WriteLine("Exists: {0}", list.Exists(s => 5 <= s.Length));
// すべての要素が"e"で終わるかどうか
Console.WriteLine("TrueForAll: {0}", list.TrueForAll(s => s.EndsWith("e")));
// すべての要素の長さが10未満かどうか
Console.WriteLine("TrueForAll: {0}", list.TrueForAll(s => s.Length < 10));
}
}
実行結果
FindLast: Eve TrueForAll: False TrueForAll: True