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.

Summary181(without us<strong>in</strong>g a dedicated thread) until it had completed, call<strong>in</strong>g the supplied delegateto handle the result. It would then call MoveNext aga<strong>in</strong>, and our method wouldcont<strong>in</strong>ue. This time we kick off two requests <strong>in</strong> parallel, and the CCR to call another delegatewith the results of both operations when they’ve both f<strong>in</strong>ished. After that, Move-Next is called for a f<strong>in</strong>al time and we get to complete the request process<strong>in</strong>g.Although it’s obviously more complicated than the synchronous version, it’s still all<strong>in</strong> one method, it will get executed <strong>in</strong> the order written, and the method itself canhold the state (<strong>in</strong> the local variables, which become state <strong>in</strong> the extra type generatedby the compiler). It’s fully asynchronous, us<strong>in</strong>g as few threads as it can get away with. Ihaven’t shown any error handl<strong>in</strong>g, but that’s also available <strong>in</strong> a sensible fashion thatforces you to th<strong>in</strong>k about the issue at appropriate places.It all takes a while to get your head around (at least unless you’ve seen cont<strong>in</strong>uationpass<strong>in</strong>gstyle code before) but the potential benefits <strong>in</strong> terms of writ<strong>in</strong>g correct, scalablecode are enormous—and it’s only feasible <strong>in</strong> such a neat way due to <strong>C#</strong> 2’s syntacticsugar around iterators and anonymous methods. The CCR hasn’t hit the ma<strong>in</strong>stream atthe time of writ<strong>in</strong>g, but it’s possible that it will become another normal part of thedevelopment toolkit 11 —and that other novel uses for iterator blocks will be thought upover time. As I said earlier, the po<strong>in</strong>t of the section is to open your m<strong>in</strong>d to possible usesof the work that the compiler can do for you beyond just simple iteration.6.5 Summary<strong>C#</strong> supports many patterns <strong>in</strong>directly, <strong>in</strong> terms of it be<strong>in</strong>g feasible to implement them<strong>in</strong> <strong>C#</strong>. However, relatively few patterns are directly supported <strong>in</strong> terms of language featuresbe<strong>in</strong>g specifically targeted at a particular pattern. In <strong>C#</strong> 1, the iterator patternwas directly supported from the po<strong>in</strong>t of view of the call<strong>in</strong>g code, but not from theperspective of the collection be<strong>in</strong>g iterated over. Writ<strong>in</strong>g a correct implementation ofIEnumerable was time-consum<strong>in</strong>g and error-prone, without be<strong>in</strong>g <strong>in</strong>terest<strong>in</strong>g. In <strong>C#</strong> 2the compiler does all the mundane work for you, build<strong>in</strong>g a state mach<strong>in</strong>e to copewith the “call-back” nature of iterators.It should be noted that iterator blocks have one aspect <strong>in</strong> common with the anonymousmethods we saw <strong>in</strong> chapter 5, even though the actual features are very different.In both cases, extra types may be generated, and a potentially complicated code transformationis applied to the orig<strong>in</strong>al source. Compare this with <strong>C#</strong> 1 where most of thetransformations for syntactic sugar (lock, us<strong>in</strong>g, and foreach be<strong>in</strong>g the most obviousexamples) were quite straightforward. We’ll see this trend toward smarter compilationcont<strong>in</strong>u<strong>in</strong>g with almost every aspect of <strong>C#</strong> 3.As well as see<strong>in</strong>g a real-life example of the use of iterators, we’ve taken a look athow one particular library has used them <strong>in</strong> a fairly radical way that has little to dowith what comes to m<strong>in</strong>d when we th<strong>in</strong>k about iteration over a collection. It’s worthbear<strong>in</strong>g <strong>in</strong> m<strong>in</strong>d that different languages have also looked at this sort of problem11 Some aspects of the CCR may also become available as part of the Parallel Extensions library described <strong>in</strong>chapter 13.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!