เมธอด RemoveAll(Predicate<T> match) ของ List<T> ลบองค์ประกอบทั้งหมดที่ทำให้ Predicate คืนค่า true คืนค่าจำนวนรายการที่ถูกลบ
ตัวอย่างที่ 1: ลบค่าเฉพาะ (500)
using System;
using System.Collections.Generic;
public class Demo
{
// Predicate: คืน true หากค่าเท่ากับ 500
private static bool Is500(int i)
{
return (i == 500);
}
public static void Main(string[] args)
{
List<int> list = new List<int>();
list.Add(100);
list.Add(500);
list.Add(300);
list.Add(400);
list.Add(500);
list.Add(600);
list.Add(500);
Console.WriteLine("List องค์ประกอบ...");
foreach (int i in list)
{
Console.WriteLine(i);
}
Console.WriteLine();
// ลบทุกตัวที่เท่ากับ 500
int removedCount = list.RemoveAll(Is500);
Console.WriteLine("List หลังลบค่า 500... (ลบไป {0} รายการ)", removedCount);
foreach (int i in list)
{
Console.WriteLine(i);
}
}
}
ผลลัพธ์
List องค์ประกอบ... 100 500 300 400 500 600 500 List หลังลบค่า 500... (ลบไป 3 รายการ) 100 300 400 600
ตัวอย่างที่ 2: ลบค่าที่หาร 10 ลงตัว (Lambda Expression)
using System;
using System.Collections.Generic;
public class Demo
{
// Predicate: คืน true หากหาร 10 ลงตัว
private static bool DivisibleBy10(int i)
{
return (i % 10) == 0;
}
public static void Main(string[] args)
{
List<int> list = new List<int>();
list.Add(100);
list.Add(295);
list.Add(355);
list.Add(450);
list.Add(550);
Console.WriteLine("List องค์ประกอบ...");
foreach (int i in list)
{
Console.WriteLine(i);
}
Console.WriteLine();
// ใช้ Method Group
int removedCount = list.RemoveAll(DivisibleBy10);
Console.WriteLine("List หลังลบค่าหาร 10 ลงตัว... (ลบไป {0} รายการ)", removedCount);
foreach (int i in list)
{
Console.WriteLine(i);
}
}
}
ผลลัพธ์
List องค์ประกอบ... 100 295 355 450 550 List หลังลบค่าหาร 10 ลงตัว... (ลบไป 3 รายการ) 295 355
รูปแบบการใช้งานต่างๆ
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// 1. Method Group (Static Method)
bool IsEven(int x) => x % 2 == 0;
int count1 = numbers.RemoveAll(IsEven);
// 2. Lambda Expression (พופולר最多)
int count2 = numbers.RemoveAll(x => x % 2 == 0);
// 3. Anonymous Method (C# 2.0 Style)
int count3 = numbers.RemoveAll(delegate(int x) { return x % 2 == 0; });
// 4. ใช้กับ Object Complex
List<Person> people = new List<Person> { ... };
int removed = people.RemoveAll(p => p.Age < 18 || string.IsNullOrEmpty(p.Name));
// 5. Predicate ที่ Capture Variable
int threshold = 50;
int count4 = numbers.RemoveAll(x => x > threshold);
ข้อสังเกตสำคัญ
RemoveAllทำงาน **In-Place** (แก้ไข List เดิม) และคืนค่าจำนวนที่ลบ- Predicate คือ `Func
` - ฟังก์ชันรับ T คืน bool - ลำดับของสมาชิกที่เหลือ **คงเดิม** (Stable - ไม่สับเปลี่ยนลำดับ)
- Complexity: O(n) - เดินผ่านทุกตัวครั้งเดียว
- หากไม่มีตัวไหนตรงเงื่อนไข คืน 0 และ List ไม่เปลี่ยนแปลง
Predicate Delegate Signature
public delegate bool Predicate<in T>(T obj);
ตัวอย่างเพิ่มเติม: ลบ String ว่างหรือ Null
List<string> items = new List<string> { "A", "", "B", null, "C", " ", "D" };
// ลบ Null, Empty, Whitespace
int removed = items.RemoveAll(s => string.IsNullOrWhiteSpace(s));
// items = {"A", "B", "C", "D"}