C# List FindAll

Description

List FindAll retrieves all the elements that match the conditions defined by the specified predicate.

Syntax


public List<T> FindAll(
  Predicate<T> match
)

Parameters

  • match - The Predicate delegate that defines the conditions of the elements to search for.

Example


using System;//from w ww  .  j  a va2s .c o m
using System.Collections;
using System.Collections.Generic;

using System.ComponentModel;

public class Product
{
    string name;
    public string Name
    {
        get { return name; }
    }

    decimal price;
    public decimal Price
    {
        get { return price; }
    }

    public Product(string name, decimal price)
    {
        this.name = name;
        this.price = price;
    }

    public static List<Product> GetSampleProducts()
    {
        List<Product> list = new List<Product>();
        list.Add(new Product("C", 9.99m));
        list.Add(new Product("A", 1.99m));
        list.Add(new Product("F", 2.99m));
        list.Add(new Product("S", 3.99m));
        return list;
    }
    public override string ToString()
    {
        return string.Format("{0}: {1}", name, price);
    }
}



class ListQueryWithDelegatesCompact
{
    static void Main()
    {
        List<Product> products = Product.GetSampleProducts();
        products.FindAll(delegate(Product p) { return p.Price > 10; })
                .ForEach(delegate(Product p) { Console.WriteLine(p); });
    }
}




















Home »
  C# Tutorial »
    System.Collections.Generic »




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack