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.

274 CHAPTER 10 Extension methodsFew of these guidel<strong>in</strong>es are particularly clear-cut—to some extent you’ll have to feelyour own way to the best use or avoidance of extension methods. It’s perfectly reasonableto never write your own extension methods at all but still use the LINQ-relatedones for the readability ga<strong>in</strong>s available there. It’s worth at least th<strong>in</strong>k<strong>in</strong>g about what’spossible, though.10.5 SummaryThe mechanical aspect of extension methods is straightforward—it’s a simple featureto describe and demonstrate. The benefits (and costs) of them are harder to talkabout <strong>in</strong> a def<strong>in</strong>itive manner—it’s a touchy-feely topic, and different people arebound to have different views on the value provided.In this chapter I’ve tried to show a bit of everyth<strong>in</strong>g—early on we looked at whatthe feature achieves <strong>in</strong> the language, before we saw some of the capabilities availablethrough the framework. In some ways, this was a relatively gentle <strong>in</strong>troduction toLINQ: we’ll be revisit<strong>in</strong>g some of the extension methods we’ve seen so far when wedelve <strong>in</strong>to query expressions <strong>in</strong> the next chapter, as well as see<strong>in</strong>g some new ones.A wide variety of methods are available with<strong>in</strong> the Enumerable class, and we’ve onlyscratched the surface. An exhaustive description with examples of all the methodswould take most of a book on its own, and it’d become rather dull. It’s much more<strong>in</strong>terest<strong>in</strong>g to come up with a scenario of your own devis<strong>in</strong>g (whether hypothetical or<strong>in</strong> a real project) and browse through MSDN to see what’s available to help you. I urgeyou to use a sandbox project of some description to play with the extension methodsprovided—it does feel like play rather than work, and you’re unlikely to want to constra<strong>in</strong>yourself to just look<strong>in</strong>g at what you need to achieve your most immediate goal.The appendix has a list of the standard query operators from LINQ, which coversmany of the methods with<strong>in</strong> Enumerable.New patterns and practices keep emerg<strong>in</strong>g <strong>in</strong> software eng<strong>in</strong>eer<strong>in</strong>g, and ideasfrom some systems often cross-poll<strong>in</strong>ate to others. That’s one of the th<strong>in</strong>gs that keepsdevelopment excit<strong>in</strong>g. Extension methods allow code to be written <strong>in</strong> a way which waspreviously unavailable <strong>in</strong> <strong>C#</strong>, creat<strong>in</strong>g fluent <strong>in</strong>terfaces and chang<strong>in</strong>g the environmentto suit our code rather than the other way around. Those are just the techniques we’velooked at <strong>in</strong> this chapter—there are bound to be <strong>in</strong>terest<strong>in</strong>g future developmentsus<strong>in</strong>g the new <strong>C#</strong> features, whether <strong>in</strong>dividually or comb<strong>in</strong>ed.The revolution obviously doesn’t end here, however. For a few calls, extensionmethods are f<strong>in</strong>e. In our next chapter we look at the real power tools: query expressionsand full-blown LINQ.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!