Search elements matching specified predicate in CSharp
Description
The following code shows how to search elements matching specified predicate.
Example
using System;/*w ww .j a v a 2 s .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", 19.99m));
list.Add(new Product("A", 11.99m));
list.Add(new Product("F", 12.99m));
list.Add(new Product("S", 13.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); });
}
}
The code above generates the following result.