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.

Extension methods <strong>in</strong> .NET 3.5263public static bool IsNullOrEmpty(this str<strong>in</strong>g text){return str<strong>in</strong>g.IsNullOrEmpty(text);}At first it seems odd to be able to call IsNullOrEmpty on a variable that is null withoutan exception be<strong>in</strong>g thrown, particularly if you’re familiar with it as a static method from.NET 2.0. In my view, code us<strong>in</strong>g the extension method is more easily understandable.For <strong>in</strong>stance, if you read the expression if (name.IsNullOrEmpty()) out loud, it saysexactly what it’s do<strong>in</strong>g. As always, experiment to see what works for you—but be awareof the possibility of other people us<strong>in</strong>g this technique if you’re debugg<strong>in</strong>g code. Don’tbe certa<strong>in</strong> that an exception will be thrown on a method call unless you’re sure it’s notan extension method! Also note that you should th<strong>in</strong>k carefully before reus<strong>in</strong>g an exist<strong>in</strong>gname for an extension method—the previous extension method could confusereaders who are only familiar with the static method from the framework.Now that we know the syntax and behavior of extension methods, we can have a lookat some examples of them, which are provided <strong>in</strong> .NET 3.5 as part of the framework.10.3 Extension methods <strong>in</strong> .NET 3.5The biggest use of extension methods <strong>in</strong> .NET 3.5 is <strong>in</strong> LINQ. Some LINQ providers havea few extension methods to help them along, but there are two classes that stand out,both of them appear<strong>in</strong>g <strong>in</strong> the System.L<strong>in</strong>q namespace: Enumerable and Queryable.These conta<strong>in</strong> many, many extension methods: most of the ones <strong>in</strong> Enumerable operateon IEnumerable and most of those <strong>in</strong> Queryable operate on IQueryable. We’llsee the purpose of IQueryable <strong>in</strong> chapter 12, but for the moment let’s concentrateon Enumerable.10.3.1 First steps with EnumerableEven just look<strong>in</strong>g at Enumerable, we’re gett<strong>in</strong>g very close to LINQ now. Indeed, a lot ofthe time you don’t need full-blown query expressions to solve a problem. Enumerablehas a lot of methods <strong>in</strong> it, and the purpose of this section isn’t to cover all of them butto give you enough of a feel for them to let you go off and experiment. It’s a real joy tojust play with everyth<strong>in</strong>g available <strong>in</strong> Enumerable—although this time it’s def<strong>in</strong>itelyworth fir<strong>in</strong>g up Visual Studio 2008 for your experiments (rather than us<strong>in</strong>g Snippy) asIntelliSense is handy for this k<strong>in</strong>d of activity.All the complete examples <strong>in</strong> this section deal with a simple situation: we start offwith a collection of <strong>in</strong>tegers and transform it <strong>in</strong> various ways. Obviously real-life situationsare likely to be somewhat more complicated, usually deal<strong>in</strong>g with bus<strong>in</strong>ess-relatedtypes. At the end of this section, I’ll present a couple of examples of just the transformationside of th<strong>in</strong>gs applied to possible bus<strong>in</strong>ess situations, with full source code availableon the book’s website—but that’s harder to play with than a straightforwardcollection of numbers. It’s worth consider<strong>in</strong>g some recent projects you’ve been work<strong>in</strong>gon as we go, however—see if you can th<strong>in</strong>k of situations where you could have madeyour code simpler or more readable by us<strong>in</strong>g the k<strong>in</strong>d of operations described here.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!