どうも、ちょげ(@chogetarou)です。
Exists()メソッドを使って、リスト(List)で条件を満たす要素の有無を確認する方法を紹介します。
方法

Exists()メソッドを使って、リスト(List)で条件を満たす要素の有無を確認するには、ラムダ式を使います。
まず、リスト(list)からExists()メソッドを呼び出します。
そして、Exists()メソッドの引数に、引数を比較対象にする条件式を返すラムダ式を指定します。
bool result = list.Exists(x => 条件式);
上記のExists()メソッドは、条件式を満たす要素がリスト(List)内にあれば「True」、無ければ「False」を返します。
使用例
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
bool result1 = numbers.Exists(item => item % 2 == 0);
bool result2 = numbers.Exists(item => item > 10);
Console.WriteLine(result1); //True
Console.WriteLine(result2); //False
}
}
コメント