Although it??™s obviously more complicated than the synchronous version, it??™s still all
in one method, it will get executed in the order written, and the method itself can
hold the state (in the local variables, which become state in the extra type generated
by the compiler). It??™s fully asynchronous, using as few threads as it can get away with. I
haven??™t shown any error handling, but that??™s also available in a sensible fashion that
forces you to think about the issue at appropriate places.
It all takes a while to get your head around (at least unless you??™ve seen continuationpassing
style code before) but the potential benefits in terms of writing correct, scalable
code are enormous??”and it??™s only feasible in such a neat way due to C# 2??™s syntactic
sugar around iterators and anonymous methods. The CCR hasn??™t hit the mainstream at
the time of writing, but it??™s possible that it will become another normal part of the
development toolkit11??”and that other novel uses for iterator blocks will be thought up
over time. As I said earlier, the point of the section is to open your mind to possible uses
of the work that the compiler can do for you beyond just simple iteration.
6.5 Summary
C# supports many patterns indirectly, in terms of it being feasible to implement them
in C#. However, relatively few patterns are directly supported in terms of language features
being specifically targeted at a particular pattern.
Pages:
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360