In this source code example, we will demonstrate how to use the FindAll method of List in C#.
The FindAll method retrieves all the elements of a list that match the conditions defined by the specified predicate. It returns a list containing all the elements that match the conditions defined by the specified predicate is found; otherwise, an empty list.C# List FindAll example
The following example finds all positive elements:using System;
using System.Collections.Generic;
public class ListDemo
{
public static void Main(string[] args)
{
var vals = new List<int> {-1, -3, 0, 1, 3, 2, 9, -4};
List<int> filtered = vals.FindAll(e => e > 0);
foreach(int e in filtered) {
Console.WriteLine(e);
}
}
}
Output:
1
3
2
9
Comments
Post a Comment