どうも、ちょげ(@chogetarou)です。
RemoveAll()メソッドを使って、リスト(List)から特定の要素をまとめて削除する方法を紹介します。
方法

RemoveAll()メソッドを使って、リスト(List)から特定の要素をまとめて削除するには、ラムダ式を使います。
まず、リスト(List)からRemoveAll()メソッドを呼び出します。
そして、RemoveAll()の引数に条件式を返すラムダ式を指定します。
list.RemoveAll(item => 条件式);
上記のRemoveAll()は、ラムダ式で返す条件式でTrueとなる要素を、呼び出したリストから削除します。
使用例
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 , 6, 7, 8, 9, 10};
numbers.RemoveAll(item => item % 2 == 0);
Console.WriteLine(String.Join(",", numbers)); //1,3,5,7,9
}
}
コメント