13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

266 CHAPTER 10 Extension methodsThat’s f<strong>in</strong>e for <strong>in</strong>stance methods, but extension methods allow static method calls tobe cha<strong>in</strong>ed together. This is one of the primary reasons for extension methods exist<strong>in</strong>g.They’re useful for other utility classes, but their true power is revealed <strong>in</strong> this ability tocha<strong>in</strong> static methods <strong>in</strong> a natural way. That’s why extension methods primarily showup <strong>in</strong> Enumerable and Queryable <strong>in</strong> .NET 3.5: LINQ is geared toward this approach todata process<strong>in</strong>g, with <strong>in</strong>formation travel<strong>in</strong>g through pipel<strong>in</strong>es constructed of <strong>in</strong>dividualoperations cha<strong>in</strong>ed together.NOTEEfficiency consideration: reorder<strong>in</strong>g method calls to avoid waste—I’m certa<strong>in</strong>lynot a fan of micro-optimization without good cause, but it’s worth look<strong>in</strong>gat the order<strong>in</strong>g of the method calls <strong>in</strong> list<strong>in</strong>g 10.8. We could haveadded the Where call after the Reverse call and achieved the same results.However, that would have wasted some effort—the Reverse call wouldhave had to work out where the even numbers should come <strong>in</strong> thesequence even though they will be discarded from the f<strong>in</strong>al result. In thiscase it’s not go<strong>in</strong>g to make much difference, but it can have a significanteffect on performance: if you can reduce the amount of wasted workwithout compromis<strong>in</strong>g readability, that’s a good th<strong>in</strong>g. That doesn’tmean you should always put filters at the start of the pipel<strong>in</strong>e, however;you need to th<strong>in</strong>k carefully about any reorder<strong>in</strong>g to make sure you’ll stillget the correct results.There are two obvious ways of writ<strong>in</strong>g the first part of list<strong>in</strong>g 10.8 without us<strong>in</strong>g thefact that Reverse and Where are extension methods. One is to use a temporary variable,which keeps the structure <strong>in</strong>tact:var collection = Enumerable.Range(0, 10);collection = Enumerable.Where(collection, x => x%2 != 0)collection = Enumerable.Reverse(collection);I hope you’ll agree that the mean<strong>in</strong>g of the code is far less clear here than <strong>in</strong> list<strong>in</strong>g 10.8.It gets even worse with the other option, which is to keep the “s<strong>in</strong>gle statement” style:var collection = Enumerable.Reverse(Enumerable.Where(Enumerable.Range(0, 10),x => x%2 != 0));The method call order appears to be reversed, because the <strong>in</strong>nermost method call(Range) will be performed first, then the others, with execution work<strong>in</strong>g its way outward.Let’s get back to our nice clean syntax but <strong>in</strong>troduce another wr<strong>in</strong>kle—we’ll transform(or project) each element <strong>in</strong> our orig<strong>in</strong>al collection, creat<strong>in</strong>g an anonymous typefor the result.10.3.3 Projections us<strong>in</strong>g the Select method and anonymous typesThe most important projection method <strong>in</strong> Enumerable is Select—it operates on anIEnumerable and projects it <strong>in</strong>to an IEnumerable by way of aFunc, which is the transformation to use on each element, specifiedLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!