Home
Manage Your Code
Snippet: Simple Lambda example (C#)
Title: Simple Lambda example Language: C#
Description: Simple Lambda example Views: 555
Author: Filip K Date Added: 3/19/2008
Copy Code  
1using System;
2using System.Collections.Generic;
3
4namespace LambdaTest
5{
6    public class Person
7    { 
8        public Person(string name, int age) 
9        { 
10            Name = name; 
11            Age = age; 
12        } 
13
14        public string Name; 
15        public int Age; 
16    }
17	
18    public class MyClass
19    {
20        public static void Main()
21        {
22            List<Person> people = new List<Person>();
23            people.Add(new Person("howard", 29));
24            people.Add(new Person("jennifer", 30));
25            people.Add(new Person("hannah", 8));
26			
27            /* anonymous delegate */
28            //List<Person> oldPeople = people.FindAll(delegate(Person p)

29            //	{ return p.Age > 10; }

30            //);

31			
32            /* lambda */
33            List<Person> oldPeople = people.FindAll(p => p.Age > 10);
34			
35            foreach (Person p in oldPeople)
36                Console.WriteLine(p.Name);
37			
38            Console.ReadLine();
39        }
40    }
41}
Notes
http://blogs.msdn.com/howard_dierking/archive/2007/01/18/lambda-lambda-lambda.aspx