C#1:循环,测试,打印
List<Product1> p1 = Product1.GetSampleProducts ();
foreach (var item in p1) { //测试和打印的硬编码方法
if (item.Price > 10.0m)
Console.WriteLine ("[Product1: Name={0},Price={1}]",item.Name,item.Price);
}
C#2:测试和打印分开进行
List<Product2> p2=Product2.GetSampleProducts();
Predicate<Product2> test=delegate(Product2 product2){ //测试和打印分开进行
return product2.Price>10.0m;
};
List<Product2> matches=p2.FindAll (test);
Action<Product2> print = Console.WriteLine;
matches.ForEach (print);
C#2:测试和打印分开进行的另外一个版本
p2.FindAll(delegate(Product2 p){ //简化过程
return p.Price>10.0m;
}).ForEach(Console.WriteLine);
C#3:用Lambda表达式来测试
List<Product3> p3 = Product3.GetSampleProducts ();
IEnumerable<Product3> nep3=p3.Where (p => p.Price > 10.0m);//Lambda
foreach (Product3 item in nep3) {
Console.WriteLine (item);
}
p3.Where (p => p.Price > 10.0m).ToList().ForEach (Console.WriteLine);//Action<Product>打印