public class Person{ private string firstName; private string lastName; private int age; public Person() { } public Person(string firstName, string lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } public override string ToString() { return String.Format("{0} {1} - {2}", FirstName, LastName, Age); } public string FirstName { get { return firstName; } set { firstName = value; } } public string LastName { get { return lastName; } set { lastName = value; } } public int Age { get { return age; } set { age = value; } }}
public static void DotNet1(){ ArrayList people = new ArrayList(); people.Add(new Person("Shawn", "Neal", 28)); people.Add(new Person("Isabella", "Neal", 16)); people.Add(new Person("John", "Smoe", 43)); ArrayList teens = new ArrayList(); foreach (Person person in people) { if (person.Age < 20 && person.Age > 12) teens.Add(person); } Console.WriteLine("The following people are teenagers:"); foreach (Person o in teens) Console.WriteLine(o.ToString());}
public static void DotNet2(){ List<Person> people = new List<Person>(); people.Add(new Person("Shawn", "Neal", 28)); people.Add(new Person("Isabella", "Neal", 16)); people.Add(new Person("John", "Smoe", 43)); List<Person> teens = people.FindAll(delegate(Person p) { return p.Age < 20 && p.Age > 12; }); Console.WriteLine("The following people are teenagers:"); foreach (Person o in teens) Console.WriteLine(o.ToString());}
public static void DotNet35(){ IList<Person> people = new List<Person>() { new Person() { Age = 28, FirstName = "Shawn", LastName = "Neal" }, new Person() { Age = 16, FirstName = "Isabella", LastName = "Neal" }, new Person() { Age = 43, FirstName = "John", LastName = "Smoe" } }; IEnumerable<Person> teens = people.Where<Person>(p => p.Age < 20 && p.Age > 12); Console.WriteLine("The following people are teenagers:"); teens.WriteAll();}
public static class MyExtension{ public static void WriteAll(this IEnumerable enumerable) { foreach (object o in enumerable) Console.WriteLine(o.ToString()); }}
Powered by: newtelligence dasBlog 2.1.8102.813
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2012, Shawn Neal
E-mail