どうも、ちょげ(@chogetarou)です。
Listの要素を条件で削除する方法を紹介します。
方法

Listの要素を条件で削除するには、RemoveAllメソッドを使います。
まず、ListからRemoveAllメソッドを呼び出します。
そして、RemoveAllメソッドの引数に条件式を返すラムダ式を指定します。
list.RemoveAll((item) => 条件式);
RemoveAllメソッドは、条件式でtrueを返した要素を全て削除します。
使用例
using System;
using System.Collections.Generic;
public class Sample{
public static void Main(){
List<int> nums = new List<int>() {1, 2,3, 4, 5};
//偶数の要素を削除
nums.RemoveAll((item) => item % 2 == 0);
foreach (var num in nums) {
Console.WriteLine(num);
}
}
}

コメント