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.

Summary31311.7 SummaryIn this chapter, we’ve looked at how LINQ to Objects and <strong>C#</strong> 3 <strong>in</strong>teract, focus<strong>in</strong>g onthe way that query expressions are first translated <strong>in</strong>to code that doesn’t <strong>in</strong>volve queryexpressions, then compiled <strong>in</strong> the usual way. We’ve seen how all query expressionsform a series of sequences, apply<strong>in</strong>g a transformation of some description at eachstep. In many cases these sequences are evaluated us<strong>in</strong>g deferred execution, fetch<strong>in</strong>gdata only when it is first required.Compared with all the other features of <strong>C#</strong> 3, query expressions look somewhatalien—more like SQL than the <strong>C#</strong> we’re used to. One of the reasons they look so oddis that they’re declarative <strong>in</strong>stead of imperative—a query talks about the features of theend result rather than the exact steps required to achieve it. This goes hand <strong>in</strong> handwith a more functional way of th<strong>in</strong>k<strong>in</strong>g. It can take a while to click, and it’s certa<strong>in</strong>lynot suitable for every situation, but where declarative syntax is appropriate it can vastlyimprove readability, as well as mak<strong>in</strong>g code easier to test and also easier to parallelize.Don’t be fooled <strong>in</strong>to th<strong>in</strong>k<strong>in</strong>g that LINQ should only be used with databases: pla<strong>in</strong><strong>in</strong>-memory manipulation of collections is common, and as we’ve seen it’s supportedvery well by query expressions and the extension methods <strong>in</strong> Enumerable.In a very real sense, you’ve seen all the new features of <strong>C#</strong> 3 now! Although wehaven’t looked at any other LINQ providers yet, we now have a clearer understand<strong>in</strong>gof what the compiler will do for us when we ask it to handle XML and SQL. The compileritself doesn’t know the difference between LINQ to Objects, LINQ to SQL, or anyof the other providers: it just follows the same rules bl<strong>in</strong>dly. In the next chapter we’llsee how these rules form the f<strong>in</strong>al piece of the LINQ jigsaw puzzle when they convertlambda expressions <strong>in</strong>to the expression trees so that the various clauses of queryexpressions can be executed on different platforms.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!