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 change9foreach (Product product <strong>in</strong> products){Console.WriteL<strong>in</strong>e (product);}The first th<strong>in</strong>g to spot <strong>in</strong> list<strong>in</strong>g 1.4 is that we’ve had to <strong>in</strong>troduce an extra type to helpus with the sort<strong>in</strong>g. That’s not a disaster, but it’s a lot of code if we only want to sort byname <strong>in</strong> one place. Next, we see the casts <strong>in</strong> the Compare method. Casts are a way oftell<strong>in</strong>g the compiler that we know more <strong>in</strong>formation than it does—and that usuallymeans there’s a chance we’re wrong. If the ArrayList we returned from GetSample-Products had conta<strong>in</strong>ed a str<strong>in</strong>g, that’s where the code would go bang—where thecomparison tries to cast the str<strong>in</strong>g to a Product.We’ve also got a cast <strong>in</strong> the code that displays the sorted list. It’s not obvious,because the compiler puts it <strong>in</strong> automatically, but the foreach loop implicitly castseach element of the list to Product. Aga<strong>in</strong>, that’s a cast we’d ideally like to get rid of,and once more generics come to the rescue <strong>in</strong> <strong>C#</strong> 2. List<strong>in</strong>g 1.5 shows the earlier codewith the use of generics as the only change.List<strong>in</strong>g 1.5 Sort<strong>in</strong>g a List us<strong>in</strong>g IComparer (<strong>C#</strong> 2)class ProductNameComparer : IComparer{public <strong>in</strong>t Compare(Product first, Product second){return first.Name.CompareTo(second.Name);}}...List products = Product.GetSampleProducts();products.Sort(new ProductNameComparer());foreach (Product product <strong>in</strong> products){Console.WriteL<strong>in</strong>e(product);}The code for the comparer <strong>in</strong> list<strong>in</strong>g 1.5 is simpler because we’re given products tostart with. No cast<strong>in</strong>g necessary. Similarly, the <strong>in</strong>visible cast <strong>in</strong> the foreach loop isgone. It’s hard to tell the difference, given that it’s <strong>in</strong>visible, but it really is gone. Honest.I wouldn’t lie to you. At least, not <strong>in</strong> chapter 1…That’s an improvement, but it would be nice to be able to sort the products by simplyspecify<strong>in</strong>g the comparison to make, without need<strong>in</strong>g to implement an <strong>in</strong>terface todo so. List<strong>in</strong>g 1.6 shows how to do precisely this, tell<strong>in</strong>g the Sort method how to comparetwo products us<strong>in</strong>g a delegate.List<strong>in</strong>g 1.6 Sort<strong>in</strong>g a List us<strong>in</strong>g Comparison (<strong>C#</strong> 2)List products = Product.GetSampleProducts();products.Sort(delegate(Product first, Product second){ return first.Name.CompareTo(second.Name); });Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!