有人能告诉我以下3个查找选项之间的差异/优势:
List<Employee> Employees = new List<Employee>();
Employee tmp = new Employee();
tmp.FirstName = "Randy";
tmp.LastName = "Jones";
Employees.Add(tmp);
tmp.FirstName = "David";
tmp.LastName = "Smith";
Employees.Add(tmp);
tmp.FirstName = "Michele";
tmp.LastName = "Morris";
Employees.Add(tmp);
// Find option 1
Employee eFound1= Employees.Find((Employee emp1) => {return emp1.LastName == "Jones";});
// Find option 2
Employee eFound2 = Employees.Find(emp2 => emp2.LastName == "Smith");
// Find option 3
Employee eFound3 = Employees.Find(
delegate(Employee emp3)
{
return emp3.LastName == "Morris";
}
);
我一直在阅读有关lambdas和谓词的内容(我怀疑它与答案有某种联系),但我不能把它们放在一起.任何启示将不胜感激!
谢谢
大卫
解决方法:
第一个是statement lambda,第二个是lambda expression,第三个是anonymous method.
它们在功能上都是等价的.