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.

270 CHAPTER 10 Extension methodsIGroup<strong>in</strong>g. There are many overloads of GroupBy, but I’ve used thesimplest one here and then selected just the key (the name of the developer) and thenumber of bugs assigned to them. After that we’ve just ordered the result to show thedevelopers with the most bugs first.One of the problems when look<strong>in</strong>g at the Enumerable class can be work<strong>in</strong>g outexactly what’s go<strong>in</strong>g on—one of the overloads of GroupBy has four type parametersand five “normal” parameters (three of which are delegates), for <strong>in</strong>stance. Don’tpanic, though—just follow the steps shown <strong>in</strong> chapter 3, assign<strong>in</strong>g different types todifferent type parameters until you’ve got a concrete example of what the methodwould look like. That usually makes it a lot easier to understand what’s go<strong>in</strong>g on.We’ll use the example of defect track<strong>in</strong>g as our sample data when we look at queryexpressions <strong>in</strong> the next chapter.These examples aren’t particularly <strong>in</strong>volved ones, but I hope you can see thepower of cha<strong>in</strong><strong>in</strong>g method calls together, where each method takes an orig<strong>in</strong>al collectionand returns another one <strong>in</strong> some form or other, whether by filter<strong>in</strong>g out somevalues, order<strong>in</strong>g them, transform<strong>in</strong>g each element, aggregat<strong>in</strong>g some values, or manyother options. In many cases, the result<strong>in</strong>g code can be read aloud and understoodimmediately—and <strong>in</strong> other situations it’s still usually a lot simpler than the equivalentcode would have been <strong>in</strong> previous versions of <strong>C#</strong>.Now that we’ve seen some of the extension methods provided for us, we’ll considerjust how and when it makes sense for you to write them yourself.10.4 Usage ideas and guidel<strong>in</strong>esLike implicit typ<strong>in</strong>g of local variables, extension methods are controversial. It wouldbe hard to claim that they make the overall aim of the code harder to understand <strong>in</strong>many cases, but at the same time they do obscure the details of what method is gett<strong>in</strong>gcalled. In the words of one of the lecturers at my university, “I’m hid<strong>in</strong>g the truth <strong>in</strong>order to show you a bigger truth”—if you believe that the most important aspect of thecode is its result, extension methods are great. If the implementation is more importantto you, then explicitly call<strong>in</strong>g a static method is clearer. Effectively, it’s the differencebetween the “what” and the “how.”We’ve already looked at us<strong>in</strong>g extension methods for utility classes and methodcha<strong>in</strong><strong>in</strong>g, but before we discuss the pros and cons further, it’s worth call<strong>in</strong>g out a coupleof aspects of this that may not be obvious.10.4.1 “Extend<strong>in</strong>g the world” and mak<strong>in</strong>g <strong>in</strong>terfaces richerWes Dyer, a former developer on the <strong>C#</strong> compiler team, has a fantastic blog 2 cover<strong>in</strong>gall k<strong>in</strong>ds of subject matter. One of his posts about extension methods 3 particularlycaught my attention. It’s called “Extend<strong>in</strong>g the World,” and it talks about how2http://blogs.msdn.com/wesdyer3http://blogs.msdn.com/wesdyer/archive/2007/03/09/extend<strong>in</strong>g-the-world.aspxLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!