13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

236 CHAPTER 9 Lambda expressions and expression trees9.2.1 Filter<strong>in</strong>g, sort<strong>in</strong>g, and actions on listsIf you remember the F<strong>in</strong>dAll method on List, it takes a Predicate andreturns a new list with all the elements from the orig<strong>in</strong>al list that match the predicate.The Sort method takes a Comparison and sorts the list accord<strong>in</strong>gly. F<strong>in</strong>ally, theForEach method takes an Action to perform on each element. List<strong>in</strong>g 9.4 useslambda expressions to provide the delegate <strong>in</strong>stance to each of these methods. Thesample data <strong>in</strong> question is just the name and year of release for various films. We pr<strong>in</strong>tout the orig<strong>in</strong>al list, then create and pr<strong>in</strong>t out a filtered list of only old films, then sortand pr<strong>in</strong>t out the orig<strong>in</strong>al list, ordered by name. (It’s <strong>in</strong>terest<strong>in</strong>g to consider howmuch more code would have been required to do the same th<strong>in</strong>g <strong>in</strong> <strong>C#</strong> 1, by the way.)List<strong>in</strong>g 9.4Manipulat<strong>in</strong>g a list of films us<strong>in</strong>g lambda expressionsclass Film{public str<strong>in</strong>g Name { get; set; }public <strong>in</strong>t Year { get; set; }public override str<strong>in</strong>g ToStr<strong>in</strong>g(){return str<strong>in</strong>g.Format("Name={0}, Year={1}", Name, Year);}}...var films = new List{new Film {Name="Jaws", Year=1975},new Film {Name="S<strong>in</strong>g<strong>in</strong>g <strong>in</strong> the Ra<strong>in</strong>", Year=1952},new Film {Name="Some Like It Hot", Year=1959},new Film {Name="The Wizard of Oz", Year=1939},new Film {Name="It's a Wonderful Life", Year=1946},new Film {Name="American Beauty", Year=1999},new Film {Name="High Fidelity", Year=2000},new Film {Name="The Usual Suspects", Year=1995}};Action pr<strong>in</strong>t = film => { Console.WriteL<strong>in</strong>e(film); };films.ForEach(pr<strong>in</strong>t);C Pr<strong>in</strong>ts orig<strong>in</strong>al listfilms.F<strong>in</strong>dAll(film => film.Year < 1960).ForEach(pr<strong>in</strong>t);films.Sort((f1, f2) => f1.Name.CompareTo(f2.Name));films.ForEach(pr<strong>in</strong>t);Createsreusablelist-pr<strong>in</strong>t<strong>in</strong>gdelegateThe first half of list<strong>in</strong>g 9.4 <strong>in</strong>volves just sett<strong>in</strong>g up the data. I would have used an anonymoustype, but it’s relatively tricky to create a generic list from a collection of anonymoustype <strong>in</strong>stances. (You can do it by creat<strong>in</strong>g a generic method that takes an arrayand converts it to a list of the same type, then pass an implicitly typed array <strong>in</strong>to thatmethod. An extension method <strong>in</strong> .NET 3.5 called ToList provides this functionalitytoo, but that would be cheat<strong>in</strong>g as we haven’t looked at extension methods yet!)DCreatesfiltered listEBSortsorig<strong>in</strong>al listLicensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!