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.

Evolution <strong>in</strong> action: examples of code change11benefits of <strong>C#</strong> 3. When the <strong>in</strong>dividual pieces of data query<strong>in</strong>g and manipulation are sosimple, larger transformations can still rema<strong>in</strong> compact and readable <strong>in</strong> one piece ofcode. That <strong>in</strong> turn encourages a more “data-centric” way of look<strong>in</strong>g at the world.We’ve seen a bit more of the power of <strong>C#</strong> 2 and 3 <strong>in</strong> this section, with quite a lot of(as yet) unexpla<strong>in</strong>ed syntax, but even without understand<strong>in</strong>g the details we can seethe progress toward clearer, simpler code. Figure 1.2 shows that evolution.<strong>C#</strong> 1Weakly typed comparatorNo delegate sort<strong>in</strong>g option<strong>C#</strong> 2Strongly typed comparatorDelegate comparisonsAnonymous methods<strong>C#</strong> 3Lambda expressionsExtension methodsOption of leav<strong>in</strong>g list unsortedFigure 1.2 Features <strong>in</strong>volved <strong>in</strong> mak<strong>in</strong>g sort<strong>in</strong>g easier <strong>in</strong> <strong>C#</strong> 2 and 3That’s it for sort<strong>in</strong>g. Let’s do a different form of data manipulation now—query<strong>in</strong>g.1.1.3 Query<strong>in</strong>g collectionsOur next task is to f<strong>in</strong>d all the elements of the list that match a certa<strong>in</strong> criterion—<strong>in</strong> particular,those with a price greater than $10. In <strong>C#</strong> 1, we need to loop around, test<strong>in</strong>g eachelement and pr<strong>in</strong>t<strong>in</strong>g it out where appropriate (see list<strong>in</strong>g 1.9).List<strong>in</strong>g 1.9 Loop<strong>in</strong>g, test<strong>in</strong>g, pr<strong>in</strong>t<strong>in</strong>g out (<strong>C#</strong> 1)ArrayList products = Product.GetSampleProducts();foreach (Product product <strong>in</strong> products){if (product.Price > 10m){Console.WriteL<strong>in</strong>e(product);}}OK, this is not difficult code to understand. However, it’s worth bear<strong>in</strong>g <strong>in</strong> m<strong>in</strong>d how<strong>in</strong>tertw<strong>in</strong>ed the three tasks are—loop<strong>in</strong>g with foreach, test<strong>in</strong>g the criterion with if,then display<strong>in</strong>g the product with Console.WriteL<strong>in</strong>e. The dependency is obviousbecause of the nest<strong>in</strong>g. <strong>C#</strong> 2 lets us flatten th<strong>in</strong>gs out a bit (see list<strong>in</strong>g 1.10).List<strong>in</strong>g 1.10 Separat<strong>in</strong>g test<strong>in</strong>g from pr<strong>in</strong>t<strong>in</strong>g (<strong>C#</strong> 2)List products = Product.GetSampleProducts();Predicate test = delegate(Product p){ return p.Price > 10m; };List matches = products.F<strong>in</strong>dAll(test);Action pr<strong>in</strong>t = delegate(Product p){ Console.WriteL<strong>in</strong>e (p); };matches.ForEach (pr<strong>in</strong>t);Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!