Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List<T>.Find (Predicates / Lambda) [closed]

Could someone tell me what the difference / advantage is between the following 3 Find options:

        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";
        }
        );

I have been reading about lambdas and predicates (which I suspect is somehow tied to the answer) but I can't put it all together. Any enlightenment would be appreciated!

Thanks David

like image 766
user902949 Avatar asked Nov 19 '13 12:11

user902949


1 Answers

The first is a statement lambda, the second one is a lambda expression and the third is an anonymous method.

They are all functionally equivalent.

like image 78
Alberto Avatar answered Sep 18 '22 19:09

Alberto