<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>Jon Skeet: Coding Blog</title><link>http://msmvps.com/blogs/jon_skeet/default.aspx</link><description>C#, .NET, Java, software development etc
**This is my personal blog. The views expressed on these pages are mine alone and not those of my employer.**</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/JonSkeetCodingBlog" /><feedburner:info uri="jonskeetcodingblog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><title>Currying vs partial function application</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/jeibq6H-CdU/currying-vs-partial-function-application.aspx</link><pubDate>Mon, 30 Jan 2012 18:32:06 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1805414</guid><dc:creator>skeet</dc:creator><slash:comments>27</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1805414</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1805414</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2012/01/30/currying-vs-partial-function-application.aspx#comments</comments><description>&lt;p&gt;This is a slightly odd post, and before you read it you should probably put yourself into one of three buckets:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Someone who doesn&amp;#39;t care too much about functional programming, and finds higher order functions tricky: feel free to skip this post entirely. &lt;/li&gt;    &lt;li&gt;Someone who knows all about functional programming, and already knows the difference between currying and partial function application: please read this post carefully and post comments about any inaccuracies you find. (Yes, the CAPTCHA is broken on Chrome; sorry.) &lt;/li&gt;    &lt;li&gt;Someone who &lt;em&gt;doesn&amp;#39;t&lt;/em&gt; know much about functional programming yet, but is interested to learn more: please take this post with a pinch of salt, and read the comments carefully. Read other articles by more experienced developers for more information. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Basically, I&amp;#39;ve been aware for a while that some people use the terms &lt;em&gt;currying&lt;/em&gt; and &lt;em&gt;partial function application&lt;/em&gt; somewhat interchangably, when they shouldn&amp;#39;t. It&amp;#39;s one of those topics (like monads) which I feel I understand to some extent, and I&amp;#39;ve decided that the best way of making sure I understand it is to try to write about it. If it helps the topic become more accessible to other developers, so much the better.&lt;/p&gt;  &lt;h3&gt;This post contains no Haskell&lt;/h3&gt;  &lt;p&gt;Almost every explanation I&amp;#39;ve ever seen of either topic has given examples in a &amp;quot;proper&amp;quot; functional language, typically Haskell. I have absolutely nothing against Haskell, but I typically find it easier to understand examples in a programming language I understand. I also find it &lt;em&gt;much&lt;/em&gt; easier to &lt;em&gt;write&lt;/em&gt; examples in a program language I understand, so all the examples in this post are going to be in C#. In fact, it&amp;#39;s all available in a &lt;a href="http://pobox.com/~skeet/csharp/blogfiles/Curry.cs"&gt;single file&lt;/a&gt; - that includes all of the examples, admittedly with a few variables renamed. Just compile and run.&lt;/p&gt;  &lt;p&gt;C# isn&amp;#39;t really a functional language - I know just about enough to understand that delegates aren&amp;#39;t really a proper substitute for first class functions. However, they&amp;#39;re good enough to demonstrate the principles involved.&lt;/p&gt;  &lt;p&gt;While it&amp;#39;s possible to demonstrate currying and partial function application using a function (method) taking a very small number of parameters, I&amp;#39;ve chosen to use 3 for clarity. Although my methods to perform the currying and partial function application will be generic (so all the types of parameters and return value are arbitrary) I&amp;#39;m using a simple function for demonstration purposes:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; SampleFunction(&lt;span class="ValueType"&gt;int&lt;/span&gt; a, &lt;span class="ValueType"&gt;int&lt;/span&gt; b, &lt;span class="ValueType"&gt;int&lt;/span&gt; c)&amp;#160; &lt;br /&gt;{&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;.Format(&lt;span class="String"&gt;&amp;quot;a={0}; b={1}; c={2}&amp;quot;&lt;/span&gt;, a, b, c);&amp;#160; &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;So far, so simple. There&amp;#39;s &lt;em&gt;nothing tricky&lt;/em&gt; about that method, so don&amp;#39;t look for anything surprising.&lt;/p&gt;  &lt;h3&gt;What&amp;#39;s it all about?&lt;/h3&gt;  &lt;p&gt;Both currying and partial function application are about converting one sort of function to another. We&amp;#39;ll use delegates as an approximation to functions, so if we want to treat the method SampleFunction as a value, we can write:&lt;/p&gt;  &lt;div class="code"&gt;Func&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;, &lt;span class="ValueType"&gt;int&lt;/span&gt;, &lt;span class="ValueType"&gt;int&lt;/span&gt;, &lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt; function = SampleFunction; &lt;/div&gt;  &lt;p&gt;This single line is useful for two reasons:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Assigning the value to a variable hammers home the point that it really is a value. A delegate instance is an object much like any other, and the value of the function variable is a reference just like any other. &lt;/li&gt;    &lt;li&gt;Method group conversions (using just the name of the method as a way of creating a delegate) doesn&amp;#39;t work terribly nicely with type inference when calling a generic method. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;We can already call the function using three arguments with no further work:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; result = function(1, 2, 3); &lt;/div&gt;  &lt;p&gt;Or equivalently:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; result = function.Invoke(1, 2, 3); &lt;/div&gt;  &lt;p&gt;(The C# compiler just converts the shorthand of the first form to the second. The IL emitted will be the same.)&lt;/p&gt;  &lt;p&gt;That&amp;#39;s fine if we&amp;#39;ve got all the arguments available at the same time, but what if we haven&amp;#39;t? To give a concrete (if somewhat contrived) example, suppose we have a logging function with three parameters (source, severity, message) and within a single class (which I&amp;#39;ll call BusinessLogic for the moment) we always want to use the same value for the &amp;quot;source&amp;quot; parameter, and we&amp;#39;d like to be able to log easily everywhere in the class specifying just the severity and message. We have a few options:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Create an adapter class which takes the log function (or more generally a logging object) and the &amp;quot;source&amp;quot; value in its constructor, stashes both in instance variables, and exposes a method with two parameters. That method just delegates to the stashed logger, using the source it&amp;#39;s remembered to supply the first argument to the three-parameter method. In BusinessLogic we create an instance of the adapter class, and stash a reference in an instance variable - then just call the two-parameter method everywhere we need to. This is probably overkill if we only need the adapter from BusinessLogic, but it&amp;#39;s reusable... so long as we&amp;#39;re trying to adapt the same logging function. &lt;/li&gt;    &lt;li&gt;Store the original logger in our BusinessLogic class, but create a helper method with two parameters. This can hard-code the value used for the &amp;quot;source&amp;quot; parameter in one place (the helper method). If we need to do this in several places, it gets annoying. &lt;/li&gt;    &lt;li&gt;Use a more general functional programming approach - probably &lt;em&gt;partial function application&lt;/em&gt; in this case. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I&amp;#39;m deliberately ignoring the discrepancy between holding a reference to &amp;quot;the logger&amp;quot; and holding a reference to &amp;quot;the logging function&amp;quot;. Obviously there&amp;#39;s a significant difference if we need to use more than one function from the logging class, but for the purposes of thinking about currying and partial function application, we&amp;#39;ll just think of &amp;quot;a logger&amp;quot; as &amp;quot;a function taking three parameters&amp;quot; (like our sample function).&lt;/p&gt;  &lt;p&gt;Now that I&amp;#39;ve given the slightly-real-world concrete example for a bit of motivation, I&amp;#39;m going to ignore it for the rest of the post, and just talk about our sample function. I don&amp;#39;t want to write a whole BusinessLogic class which pretends to do something useful; I&amp;#39;m sure you can perform the appropriate mental conversion from &amp;quot;the sample function&amp;quot; to &amp;quot;something I might actually want to do&amp;quot;.&lt;/p&gt;  &lt;h3&gt;Partial Function Application&lt;/h3&gt;  &lt;p&gt;The purpose of partial function application is to take a function with N parameters and a value for one of those parameters, and return a function with N-1 parameters, such that calling the result will assemble all the required values appropriately (the 1 argument given to the partial application operation itself, and the N-1 arguments given to the returned function). So in code form, these two calls should be equivalent for our 3-parameter method:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="InlineComment"&gt;// Normal call &lt;/span&gt;    &lt;br /&gt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; result1 = function(1, 2, 3);     &lt;br /&gt;    &lt;br /&gt;&lt;span class="InlineComment"&gt;// Call via partial application &lt;/span&gt;    &lt;br /&gt;Func&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;, &lt;span class="ValueType"&gt;int&lt;/span&gt;, &lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt; partialFunction = ApplyPartial(function, 1);&amp;#160; &lt;br /&gt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; result2 = partialFunction(2, 3); &lt;/div&gt;  &lt;p&gt;In this case I&amp;#39;ve implemented partial application with a single parameter, and chosen the first one - you &lt;em&gt;could&lt;/em&gt; write an ApplyPartial method which took more arguments to apply, or which used them somewhere else in the final function execution. I believe that picking off parameters one at a time, from the start, is the most conventional approach.&lt;/p&gt;  &lt;p&gt;Thanks to anonymous functions (a lambda expression in this case, but an anonymous method wouldn&amp;#39;t be much more verbose), the implementation of ApplyPartial is simple:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;static&lt;/span&gt; Func&amp;lt;T2, T3, TResult&amp;gt; ApplyPartial&amp;lt;T1, T2, T3, TResult&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; (Func&amp;lt;T1, T2, T3, TResult&amp;gt; function, T1 arg1)&amp;#160; &lt;br /&gt;{&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; (b, c) =&amp;gt; function(arg1, b, c);&amp;#160; &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;The generics make the method look more complicated than it really is. Note that the lack of higher order types in C# means that you&amp;#39;d need a method like this for &lt;em&gt;every&lt;/em&gt; delegate you wanted to use - if you wanted a version for a function which started with four parameters, you&amp;#39;d need an ApplyPartial&amp;lt;T1, T2, T3, T4, TResult&amp;gt; method etc. You&amp;#39;d probably also want a parallel set of methods for the Action delegate family.&lt;/p&gt;  &lt;p&gt;The final thing to note is that assuming we had all of these methods, we could perform partial function application again - even potentially down to a parameterless function if we wanted, like this:&lt;/p&gt;  &lt;div class="code"&gt;Func&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;, &lt;span class="ValueType"&gt;int&lt;/span&gt;, &lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt; partial1 = ApplyPartial(function, 1);&amp;#160; &lt;br /&gt;Func&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;, &lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt; partial2 = ApplyPartial(partial1, 2);&amp;#160; &lt;br /&gt;Func&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt; partial3 = ApplyPartial(partial2, 3);&amp;#160; &lt;br /&gt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; result = partial3(); &lt;/div&gt;  &lt;p&gt;Again, only the final line would actually invoke the original function.&lt;/p&gt;  &lt;p&gt;Okay, so that&amp;#39;s partial function application. That&amp;#39;s &lt;em&gt;relatively&lt;/em&gt; straightforward. Currying is slightly harder to get your head round, in my view.&lt;/p&gt;  &lt;h3&gt;Currying&lt;/h3&gt;  &lt;p&gt;Whereas partial function application converts a function with N parameters into a function with N-1 parameters by applying one argument, currying effectively decomposes the function into functions taking a single parameter. We don&amp;#39;t pass any arguments into the Curry method itself:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Curry(f) returns a function f1 such that... &lt;/li&gt;    &lt;li&gt;f1(a) returns a function f2 such that... &lt;/li&gt;    &lt;li&gt;f2(b) returns a function f3 such that... &lt;/li&gt;    &lt;li&gt;f3(c) invokes f(a, b, c) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;(Again, note that this is specific to our three-parameter function - but hopefully it&amp;#39;s obvious how it would extend to other signatures.)&lt;/p&gt;  &lt;p&gt;To give our &amp;quot;equivalence&amp;quot; example again, we can write:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="InlineComment"&gt;// Normal call &lt;/span&gt;    &lt;br /&gt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; result1 = function(1, 2, 3);     &lt;br /&gt;    &lt;br /&gt;&lt;span class="InlineComment"&gt;// Call via currying &lt;/span&gt;    &lt;br /&gt;Func&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;, Func&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;, Func&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;, &lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt;&amp;gt;&amp;gt; f1 = Curry(function);&amp;#160; &lt;br /&gt;Func&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;, Func&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;, &lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt;&amp;gt; f2 = f1(1);&amp;#160; &lt;br /&gt;Func&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;, &lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt; f3 = f2(2);&amp;#160; &lt;br /&gt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; result2 = f3(3);     &lt;br /&gt;    &lt;br /&gt;&lt;span class="InlineComment"&gt;// Or to do make all the calls together... &lt;/span&gt;    &lt;br /&gt;&lt;span class="Linq"&gt;var&lt;/span&gt; curried = Curry(function);&amp;#160; &lt;br /&gt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; result3 = curried(1)(2)(3); &lt;/div&gt;  &lt;p&gt;The difference between the latter examples shows one reason why functional languages often have good type inference and compact representations of function types: that declaration of f1 is pretty fearsome.&lt;/p&gt;  &lt;p&gt;Now that we know what the Curry method is meant to do, it&amp;#39;s actually surprisingly simple to implement. Indeed, all we need to do is translate the bullet points above into lambda expressions. It&amp;#39;s a thing of beauty:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;static&lt;/span&gt; Func&amp;lt;T1, Func&amp;lt;T2, Func&amp;lt;T3, TResult&amp;gt;&amp;gt;&amp;gt; Curry&amp;lt;T1, T2, T3, TResult&amp;gt;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; (Func&amp;lt;T1, T2, T3, TResult&amp;gt; function)&amp;#160; &lt;br /&gt;{&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; a =&amp;gt; b =&amp;gt; c =&amp;gt; function(a, b, c);&amp;#160; &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;If you want to add some brackets to make it clearer for you, feel free - personally I think it just adds clutter. Either way, we get what we want. (It&amp;#39;s worth thinking about how annoying it would be to write that without lambda expressions or anonymous methods. Not &lt;em&gt;hard&lt;/em&gt;, just annoying.)&lt;/p&gt;  &lt;p&gt;So that&amp;#39;s currying. I think. Maybe.&lt;/p&gt;  &lt;h3&gt;Conclusion&lt;/h3&gt;  &lt;p&gt;I can&amp;#39;t say I&amp;#39;ve ever knowingly used currying, whereas I suspect some bits of &lt;a href="http://noda-time.googlecode.com"&gt;Noda Time&lt;/a&gt;&amp;#39;s text parsing effectively use partial functional application. (If anyone really wants me to dig in and check, I&amp;#39;ll do so.)&lt;/p&gt;  &lt;p&gt;I really hope I&amp;#39;ve got the difference between them right here - it &lt;em&gt;feels&lt;/em&gt; right, in that the two are clearly related, but also quite distinct. Now that we&amp;#39;ve reached the end, think about how that difference manifests itself when there are only two parameters, and hopefully you&amp;#39;ll see why I chose to use three :)&lt;/p&gt;  &lt;p&gt;My gut feeling is that currying is a more useful concept in an academic context, whereas partial functional application is more useful in practice. However, that&amp;#39;s the gut feeling of someone who hasn&amp;#39;t really used a functional language in anger. If I ever &lt;em&gt;really&lt;/em&gt; get round to using F#, maybe I&amp;#39;ll do a follow-up post. For now, I&amp;#39;m hoping that my trusty smart readers can provide useful thoughts for others.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1805414" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/jeibq6H-CdU" height="1" width="1"/&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/General/default.aspx">General</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2012/01/30/currying-vs-partial-function-application.aspx</feedburner:origLink></item><item><title>Eduasync part 19: ordering by completion, ahead of time...</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/t0SgYtV4iDA/eduasync-part-19-ordering-by-completion-ahead-of-time.aspx</link><pubDate>Mon, 16 Jan 2012 22:22:43 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1804970</guid><dc:creator>skeet</dc:creator><slash:comments>13</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1804970</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1804970</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2012/01/16/eduasync-part-19-ordering-by-completion-ahead-of-time.aspx#comments</comments><description>&lt;p&gt;Today&amp;#39;s post involves the &lt;a href="http://code.google.com/p/eduasync/source/browse/#hg%2Fsrc%2FMagicOrdering"&gt;MagicOrdering project&lt;/a&gt; in source control (project 28).&lt;/p&gt;  &lt;p&gt;When I wrote &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2011/11/22/eduasync-part-16-example-of-composition-majority-voting.aspx"&gt;part 16 of Eduasync&lt;/a&gt;, showing composition in the form of majority voting, one reader mailed me a &lt;em&gt;really&lt;/em&gt; interesting suggestion. We don&amp;#39;t really need to wait for &lt;em&gt;any&lt;/em&gt; of the tasks to complete on each iteration of the loop - we only need to wait for the &lt;em&gt;next&lt;/em&gt; task to complete. Now that sounds impossible - sure, it&amp;#39;s great if we know the completion order of the tasks, but half the point of asynchrony is that many things can be happening at once, and we don&amp;#39;t know when they&amp;#39;ll complete. However, it&amp;#39;s not as silly as it sounds.&lt;/p&gt;  &lt;p&gt;If you give me a collection of tasks, I&amp;#39;ll give you back another collection of tasks which will return the same results - but I&amp;#39;ll order them so that the first returned task will have the same result as whichever of your original tasks completes first, and the second returned task will have the same result as whichever of your original tasks completes second, and so on. They won&amp;#39;t be the &lt;em&gt;same&lt;/em&gt; tasks as you gave me, reordered - but they&amp;#39;ll be tasks with the same results. I&amp;#39;ll propagate cancellation, exceptions and so on.&lt;/p&gt;  &lt;p&gt;It still sounds impossible... until you realize that I don&amp;#39;t have to associate one of my returned tasks with one of your original tasks &lt;em&gt;until it has completed&lt;/em&gt;. Before anything has completed, all the tasks look the same. The trick is that as soon as I see one of your tasks complete, I can fetch the result and propagate it to the first of the tasks I&amp;#39;ve returned to you, using TaskCompletionSource&amp;lt;T&amp;gt;. When the second of your tasks completes, I propagate the result to the second of the returned tasks, etc. This is all quite easy using &lt;a href="http://msdn.microsoft.com/en-us/library/dd235663.aspx"&gt;Task&amp;lt;T&amp;gt;.ContinueWith&lt;/a&gt; - barring a few caveats I&amp;#39;ll mention later on.&lt;/p&gt;  &lt;p&gt;Once we&amp;#39;ve built a method to do this, we can then &lt;em&gt;really easily&lt;/em&gt; build a method which is the async equivalent of &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.foreach.aspx"&gt;Parallel.ForEach&lt;/a&gt; (and indeed you could write multiple methods for the various overloads). This will execute a specific action on each task in turn, as it completes... it&amp;#39;s &lt;em&gt;like&lt;/em&gt; repeatedly calling &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.whenany(v=vs.110).aspx"&gt;Task.WhenAny&lt;/a&gt;, but we only actually need to wait for one task at a time, because we know that the first task in our &amp;quot;completion ordered&amp;quot; collection will be the first one to complete (duh).&lt;/p&gt;  &lt;h3&gt;Show me the code!&lt;/h3&gt;  &lt;p&gt;Enough description - let&amp;#39;s look at how we&amp;#39;ll demonstrate both methods, and then how we implement them.&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;async&lt;/span&gt; Task PrintDelayedRandomTasksAsync()     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Random rng = &lt;span class="Keyword"&gt;new&lt;/span&gt; Random();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Linq"&gt;var&lt;/span&gt; values = Enumerable.Range(0, 10).Select(_ =&amp;gt; rng.Next(3000)).ToList();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Initial order: {0}&amp;quot;&lt;/span&gt;, &lt;span class="ReferenceType"&gt;string&lt;/span&gt;.Join(&lt;span class="String"&gt;&amp;quot; &amp;quot;&lt;/span&gt;, values));     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Linq"&gt;var&lt;/span&gt; tasks = values.Select(DelayAsync);     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Linq"&gt;var&lt;/span&gt; ordered = OrderByCompletion(tasks);     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;In order of completion:&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;await&lt;/span&gt; ForEach(ordered, Console.WriteLine);     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;&lt;span class="XmlComment"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span class="XmlComment"&gt;/// Returns a task which delays (asynchronously) by the given number of milliseconds,&lt;/span&gt;     &lt;br /&gt;&lt;span class="XmlComment"&gt;/// then return that same number back.&lt;/span&gt;     &lt;br /&gt;&lt;span class="XmlComment"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;async&lt;/span&gt; Task&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt; DelayAsync(&lt;span class="ValueType"&gt;int&lt;/span&gt; delayMillis)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;await&lt;/span&gt; TaskEx.Delay(delayMillis);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; delayMillis;     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;The idea is that we&amp;#39;re going to create 10 tasks which each just wait for some random period of time, and return the same time period back. We&amp;#39;ll create them in any old order - but obviously they should complete in (at least roughly) the same order as the returned numbers.&lt;/p&gt;  &lt;p&gt;Once we&amp;#39;ve created the collection of tasks, we&amp;#39;ll call OrderByCompletion to create a &lt;em&gt;second&lt;/em&gt; collection of tasks, returning the same results but this time in completion order - so ordered.ElementAt(0) will be the first task to complete, for example.&lt;/p&gt;  &lt;p&gt;Finally, we call ForEach and pass in the ordered task collection, along with Console.WriteLine as the action to take with each value. We await the resulting Task to mimic blocking until the foreach loop has finished. Note that we &lt;em&gt;could&lt;/em&gt; make this a non-async method and just return the task returned by ForEach, given that that&amp;#39;s our only await expression and it&amp;#39;s right at the end of the method. This would be marginally faster, too - there&amp;#39;s no need to build an extra state machine. See &lt;a href="http://msdn.microsoft.com/en-us/magazine/hh456402.aspx"&gt;Stephen Toub&amp;#39;s article about async performance&lt;/a&gt; for more information.&lt;/p&gt;  &lt;h3&gt;ForEach&lt;/h3&gt;  &lt;p&gt;I&amp;#39;d like to get ForEach out of the way first, as it&amp;#39;s so simple: it&amp;#39;s literally just iterating over the tasks, awaiting them and propagating the result to the action. We get the &amp;quot;return a task which will wait until we&amp;#39;ve finished&amp;quot; for free by virtue of making it an async method.&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="XmlComment"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span class="XmlComment"&gt;/// Executes the given action on each of the tasks in turn, in the order of&lt;/span&gt;     &lt;br /&gt;&lt;span class="XmlComment"&gt;/// the sequence. The action is passed the result of each task.&lt;/span&gt;     &lt;br /&gt;&lt;span class="XmlComment"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;async&lt;/span&gt; Task ForEach&amp;lt;T&amp;gt;(IEnumerable&amp;lt;Task&amp;lt;T&amp;gt;&amp;gt; tasks, Action&amp;lt;T&amp;gt; action)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;foreach&lt;/span&gt; (&lt;span class="Linq"&gt;var&lt;/span&gt; task &lt;span class="Statement"&gt;in&lt;/span&gt; tasks)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; T value = &lt;span class="Modifier"&gt;await&lt;/span&gt; task;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; action(value);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;Simple, right? Let&amp;#39;s get onto the meat...&lt;/p&gt;  &lt;h5&gt;OrderByCompletion&lt;/h5&gt;  &lt;p&gt;This is the tricky bit, and I&amp;#39;ve actually split it into two methods to make it slightly easier to comprehend. The PropagateResult method feels like it could be useful in other composition methods, too.&lt;/p&gt;  &lt;p&gt;The basic plan is:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Copy the input tasks to a list: we need to work out how many there are &lt;em&gt;and&lt;/em&gt; iterate over them, so let&amp;#39;s make sure we only iterate once &lt;/li&gt;    &lt;li&gt;Create a collection of TaskCompletionSource&amp;lt;T&amp;gt; references, one for each input task. Note that we&amp;#39;re not associating any particular input task with any particular completion source - we just need the same number of them &lt;/li&gt;    &lt;li&gt;Declare an integer to keep track of &amp;quot;the next available completion source&amp;quot; &lt;/li&gt;    &lt;li&gt;Attach a continuation to each input task which will be increment the counter we&amp;#39;ve just declared, and propagate the just-completed task&amp;#39;s status &lt;/li&gt;    &lt;li&gt;Return a view onto the collection of TaskCompletionSource&amp;lt;T&amp;gt; values, projecting each one to its Task property &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Once you&amp;#39;re happy with the idea, the implementation isn&amp;#39;t too surprising (although it &lt;em&gt;is&lt;/em&gt; quite long):&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="XmlComment"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span class="XmlComment"&gt;/// Returns a sequence of tasks which will be observed to complete with the same set&lt;/span&gt;     &lt;br /&gt;&lt;span class="XmlComment"&gt;/// of results as the given input tasks, but in the order in which the original tasks complete.&lt;/span&gt;     &lt;br /&gt;&lt;span class="XmlComment"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt; IEnumerable&amp;lt;Task&amp;lt;T&amp;gt;&amp;gt; OrderByCompletion&amp;lt;T&amp;gt;(IEnumerable&amp;lt;Task&amp;lt;T&amp;gt;&amp;gt; inputTasks)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Copy the input so we know it&amp;#39;ll be stable, and we don&amp;#39;t evaluate it twice&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Linq"&gt;var&lt;/span&gt; inputTaskList = inputTasks.ToList();     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Could use Enumerable.Range here, if we wanted...&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Linq"&gt;var&lt;/span&gt; completionSourceList = &lt;span class="Keyword"&gt;new&lt;/span&gt; List&amp;lt;TaskCompletionSource&amp;lt;T&amp;gt;&amp;gt;(inputTaskList.Count);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;for&lt;/span&gt; (&lt;span class="ValueType"&gt;int&lt;/span&gt; i = 0; i &amp;lt; inputTaskList.Count; i++)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; completionSourceList.Add(&lt;span class="Keyword"&gt;new&lt;/span&gt; TaskCompletionSource&amp;lt;T&amp;gt;());     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// At any one time, this is &amp;quot;the index of the box we&amp;#39;ve just filled&amp;quot;.&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// It would be nice to make it nextIndex and start with 0, but Interlocked.Increment&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// returns the incremented value...&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ValueType"&gt;int&lt;/span&gt; prevIndex = -1;     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// We don&amp;#39;t have to create this outside the loop, but it makes it clearer&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// that the continuation is the same for all tasks.&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Action&amp;lt;Task&amp;lt;T&amp;gt;&amp;gt; continuation = completedTask =&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ValueType"&gt;int&lt;/span&gt; index = Interlocked.Increment(&lt;span class="MethodParameter"&gt;ref&lt;/span&gt; prevIndex);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Linq"&gt;var&lt;/span&gt; source = completionSourceList[index];     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PropagateResult(completedTask, source);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; };     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;foreach&lt;/span&gt; (&lt;span class="Linq"&gt;var&lt;/span&gt; inputTask &lt;span class="Statement"&gt;in&lt;/span&gt; inputTaskList)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// TODO: Work out whether TaskScheduler.Default is really the right one to use.&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; inputTask.ContinueWith(continuation,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CancellationToken.None,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; TaskContinuationOptions.ExecuteSynchronously,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; TaskScheduler.Default);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; completionSourceList.Select(source =&amp;gt; source.Task);     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;&lt;span class="XmlComment"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span class="XmlComment"&gt;/// Propagates the status of the given task (which must be completed) to a task completion source&lt;/span&gt;     &lt;br /&gt;&lt;span class="XmlComment"&gt;/// (which should not be).&lt;/span&gt;     &lt;br /&gt;&lt;span class="XmlComment"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; PropagateResult&amp;lt;T&amp;gt;(Task&amp;lt;T&amp;gt; completedTask,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; TaskCompletionSource&amp;lt;T&amp;gt; completionSource)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;switch&lt;/span&gt; (completedTask.Status)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;case&lt;/span&gt; TaskStatus.Canceled:     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; completionSource.TrySetCanceled();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;break&lt;/span&gt;;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;case&lt;/span&gt; TaskStatus.Faulted:     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; completionSource.TrySetException(completedTask.Exception.InnerExceptions);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;break&lt;/span&gt;;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;case&lt;/span&gt; TaskStatus.RanToCompletion:     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; completionSource.TrySetResult(completedTask.Result);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;break&lt;/span&gt;;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;default&lt;/span&gt;:     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// TODO: Work out whether this is really appropriate. Could set&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// an exception in the completion source, of course...&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;throw&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; ArgumentException(&lt;span class="String"&gt;&amp;quot;Task was not completed&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;You&amp;#39;ll notice there are a couple of TODO comments there. The exception in PropagateResult really &lt;em&gt;shouldn&amp;#39;t&lt;/em&gt; happen - the continuation shouldn&amp;#39;t be called when the task hasn&amp;#39;t completed. I still need to think carefully about how tasks should propagate exceptions though.&lt;/p&gt;  &lt;p&gt;The arguments to ContinueWith are more tricky: working through my TimeMachine class and some unit tests with Bill Wagner last week showed just how little I know about how SynchronizationContext, the task awaiters, task schedulers, and TaskContinuationOptions.ExecuteSynchronously all interact. I would &lt;em&gt;definitely&lt;/em&gt; need to look into that more deeply before TimeMachine was really ready for heavy use... which means &lt;em&gt;you&lt;/em&gt; should probably be looking at the TPL in more depth too.&lt;/p&gt;  &lt;h3&gt;Conclusion&lt;/h3&gt;  &lt;p&gt;Sure enough, when you run the code, the results appear in order, as the tasks complete. Here&amp;#39;s one sample of the output:&lt;/p&gt;  &lt;div class="code"&gt;Initial order: 335 468 1842 1991 2512 2603 270 2854 1972 1327   &lt;br /&gt;In order of completion:    &lt;br /&gt;270    &lt;br /&gt;335    &lt;br /&gt;468    &lt;br /&gt;1327    &lt;br /&gt;1842    &lt;br /&gt;1972    &lt;br /&gt;1991    &lt;br /&gt;2512    &lt;br /&gt;2603    &lt;br /&gt;2854 &lt;/div&gt;  &lt;p&gt;TODOs aside, the code in this post is remarkable (which I can say with modesty, as I&amp;#39;ve only refactored it from the code sent to me by another reader and Stephen Toub). It makes me smile every time I &lt;em&gt;think&lt;/em&gt; about the seemingly-impossible job it accomplishes. I suspect this approach could be useful in any number of composition blocks - it&amp;#39;s definitely one to remember.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1804970" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/t0SgYtV4iDA" height="1" width="1"/&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+5/default.aspx">C# 5</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/async/default.aspx">async</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Eduasync/default.aspx">Eduasync</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2012/01/16/eduasync-part-19-ordering-by-completion-ahead-of-time.aspx</feedburner:origLink></item><item><title>Coding in the style of Glee</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/8MoNrulucZg/coding-in-the-style-of-glee.aspx</link><pubDate>Sun, 15 Jan 2012 20:07:55 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1804883</guid><dc:creator>skeet</dc:creator><slash:comments>0</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1804883</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1804883</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2012/01/15/coding-in-the-style-of-glee.aspx#comments</comments><description>&lt;p&gt;As &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2012/01/15/codemash-2012-report.aspx"&gt;previously mentioned&lt;/a&gt;, at CodeMash 2012 I gave a very silly Pecha Kucha talk entitled &amp;quot;Coding in the style of Glee&amp;quot;. The video is &lt;a href="http://youtu.be/xDTJ6iVgMWs?hd=1"&gt;on YouTube&lt;/a&gt;, or can be seen embedded below:&lt;/p&gt; &lt;iframe height="315" src="http://www.youtube.com/embed/xDTJ6iVgMWs" frameborder="0" width="560"&gt;&lt;/iframe&gt;  &lt;p&gt;(There&amp;#39;s also &lt;a href="http://youtu.be/11u_DwaT3Zw?hd=1"&gt;another YouTube video&lt;/a&gt; from a different angle.)&lt;/p&gt;  &lt;p&gt;This post gives the 20 slides (which were just text; no fancy pictures unlike my competitors) and what I &lt;em&gt;meant&lt;/em&gt; to say about them. (Edited very slightly to remove a couple of CodeMash-specific in-jokes.) Don&amp;#39;t forget that each slide was only up for 20 seconds.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Coding in the style of Glee&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;As you may know, I’m from the UK, and it’s wonderful to be here. This is my first US conference, so it’s great to be in the country which has shared with the world its most marvellous cultural output: the Fox show, Glee.&lt;/p&gt;  &lt;p&gt;At first I watched it just for surface story – but now I know better – I know that really, the songs are all about the culture and practice of coding.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;(It isn&amp;#39;t easy) Bein&amp;#39; Green&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;When I started coding, it was on a ZX Spectrum, in Basic. It was hard, but the computer came with a great manual. I later learned C from a ringbinder of some course or other – and entirely failed to understand half the language. Of course, this was before Stack Overflow, when it was really hard being a newbie – where could you turn for information?&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Getting to know you&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Over time I became semi-competent in C, with the help of friends. But learning is a constant process, of course – getting to know new languages and platforms is just part of a good dev&amp;#39;s life every day.&lt;/p&gt;  &lt;p&gt;Learning itself is a skill – how similar it is to getting to know small children, I leave to your imagination.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Man in the Mirror&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Glee doesn’t just talk about the coding experience, of course – it talks about specific technologies. This Michael Jackson song is talking about reflection, of course. Although the idea wasn’t new in Java, it was new to me – and now it would be almost unthinkable to come up with a new platform which didn’t let you find out about what was in the code.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Bridge over Troubled Water&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Another technology covered beautifully by Glee is the interop. We’re in a big world, and we always need to talk to other systems. Whether it’s over JNI (heaven help you), P/Invoke, SOAP, REST – whatever, I hope next time you connect to another system, you’ll hear this haunting Simon and Garfunkel melody in the background.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;I will survive&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;And who could forget persistence frameworks. I’m not sure whether Gloria Gaynor had Hibernate and the Entity Framework in mind when she sang this, but I’m utterly convinced that the Glee writers did. When you submit your data, it’s just got to survive – what else would you want?&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;You can&amp;#39;t always get what you want&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;We’d all like perfect specifications, reliable libraries, ideal languages, etc – but that’s just not going to happen. It’s possible that of course you won’t get what you need – even if you try real hard. But hey, you might just. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Lean on me (or Agile on me)&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;(I didn&amp;#39;t actually have notes written for this one. Copied from the video.)&lt;/p&gt;  &lt;p&gt;Glee sympathizes with you - but it also have a bit of an answer: lean on me. Lean and agile development, so we can adapt to constantly changing specifications, and eventually we will have something that is useful. Maybe nothing like what we initially envisaged, but it will be something useful.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Losing my Religion&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Of course, we don’t always stay in love with a platform. I’d like to dedicate this slight to Enterprise Java. Fortunately I never had to deal with Enterprise Java Beans, but I “enjoyed” enough other J2EE APIs to make me yearn for a world without BeanProcessorFactoryFactories.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Anything Goes&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Now I’m pretty conservative – only in terms of coding, mind you. I’m a statically typed language guy. But Glee celebrates dynamic languages too – languages where really, anything goes until you try to execute it. Even though I haven’t gone down the dynamic route myself much, it’s important that we all welcome the diversity of languages and platforms we have.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Get Happy&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Along with the rise of dynamic languages, we seem to have seen a rise of happy developers. We’ve always had enthusiastic developers, but there’s a certain air about your typical Ruby on Rails developer which feels new to me. Again, I’m not a Ruby fan myself – but it’s always nice to see other happy people, and maybe one day I’ll see the light.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Bust your Windows&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;I don’t know what I can say about this song. Do the Glee writers have it in for Microsoft? I don’t remember “Bust your OSX” or “Bust your Linux” for example. Only Windows is targeted here.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The Safety Dance&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;One big change for me since joining Google is increased awareness of the need for redundancy – the intricate dance we need to perform to create a service which will stay up no matter what. Where redundancy is a dirty word in most of industry, as developers we celebrate it – and will do anything we can to avoid…&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The Only Exception&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;… a single point of failure.&lt;/p&gt;  &lt;p&gt;(Yes, that really is all I&amp;#39;d prepared for that slide. Hence the need for improvisation.)&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Telephone&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;(From video.) Glee celebrates the rise of phone apps. Who these days could be unaware of the importance of the development of mobile applications? And obviously, we can credit the iPhone for that, but since the iPhone, and just smart phone apps, we&amp;#39;ve also started...&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;U Can&amp;#39;t Touch This&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;(From video.) Tablets! And touch screen devices of all kinds. So Windows 8 - very touch-based, and sooner or later we&amp;#39;re all going to have to get with it. I don&amp;#39;t do UIs, I&amp;#39;ve never done a touch UI in my life, I have no idea how it works. But clearly it&amp;#39;s one of the ways forward.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Forget You&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;As smart phones and tablets become more ubiquitous and more bound to us as people, privacy has become more important. Glee gave us a timely reminder of the reverse of the persistence early on: we need to be able to forget about users, as well as remember them.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;(A)waiting for a girl like you&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;(From video.) I&amp;#39;d like to leave on an up-note, so: I&amp;#39;m clearly very, very excited (really, really excited) about C# 5 and its await keyword so I ask you - I beg you - be &lt;em&gt;excited&lt;/em&gt; about development. And always bear in mind your users.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;My life would suck without you&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Users rule our world. Can’t live without them, can’t shoot ‘em.&lt;/p&gt;  &lt;p&gt;Celebrate – we do stuff to make users really happy! This is awesome! We should be thrilled!&lt;/p&gt;  &lt;p&gt;(Even for enterprise apps, we’re doing useful stuff. Honest.)&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Don&amp;#39;t stop believing&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;(From video.) So to sum up: have fun, keep learning, really, really enjoy what you&amp;#39;re doing, and... don&amp;#39;t stop!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1804883" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/8MoNrulucZg" height="1" width="1"/&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/General/default.aspx">General</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Wacky+Ideas/default.aspx">Wacky Ideas</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Speaking+engagements/default.aspx">Speaking engagements</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2012/01/15/coding-in-the-style-of-glee.aspx</feedburner:origLink></item><item><title>CodeMash 2012 report</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/U1lOnDVXmy4/codemash-2012-report.aspx</link><pubDate>Sun, 15 Jan 2012 19:38:42 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1804880</guid><dc:creator>skeet</dc:creator><slash:comments>7</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1804880</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1804880</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2012/01/15/codemash-2012-report.aspx#comments</comments><description>&lt;p&gt;I&amp;#39;m nearly home - on a bus back from Heathrow airport to Reading - returning from &lt;a href="http://codemash.org/"&gt;CodeMash 2012&lt;/a&gt;. This was my first US conference, and I had a &lt;em&gt;wonderful&lt;/em&gt; time. It was pretty densely packed in terms of presenting / recording for me:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;I presented two back-to-back sessions jointly with Bill Wagner, on async. These went down really well (particularly Bill&amp;#39;s genius idea of using the Doctor Who quote about time being a &amp;quot;big ball of wibbly-wobbly, timey-wimey stuff&amp;quot;) and were &lt;em&gt;great&lt;/em&gt; fun to give. Bill&amp;#39;s a class act, and I think we got the balance between use and underpinnings about right. &lt;/li&gt;    &lt;li&gt;I recorded a &lt;a href="http://hanselminutes.com/"&gt;podcast with Scott Hanselman&lt;/a&gt; (we were going to record two, but the first one ended up being longer than expected) &lt;/li&gt;    &lt;li&gt;I presented a talk on &amp;quot;C#&amp;#39;s Greatest Mistakes&amp;quot; which ended up being somewhere between a discussion on language design, and a demonstration of surprising &amp;quot;features&amp;quot; of C#. It overran by 15 minutes without me coming &lt;em&gt;close&lt;/em&gt; to running out of things to say, but hopefully it was useful. It was a somewhat rambly session, but at least I warned folks of that up-front. It would be nice to be able to present the same sort of material in a really &amp;quot;tight&amp;quot; way, but I&amp;#39;m just not sure how to. &lt;/li&gt;    &lt;li&gt;I gave a 20x20 &amp;quot;Pecha Kucha&amp;quot; talk called &amp;quot;Coding in the style of Glee&amp;quot; as the silliest topic I could come up with on short notice. This was absolutely terrifying and extremely silly. I only came third in the contest (and the winner, Leon, was simply &lt;em&gt;phenomenal&lt;/em&gt;) but I was happy that I&amp;#39;d only embarrassed myself about as much as I&amp;#39;d expected to. The &lt;a href="http://youtu.be/xDTJ6iVgMWs?hd=1"&gt;YouTube video&lt;/a&gt; of this is already up, and I&amp;#39;ll write a blog post with the slide titles and what I was &lt;em&gt;trying&lt;/em&gt; to say in them :)&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Unfortunately due to last minute async prep and desperately trying to cobble together slides for the Glee talk, I didn&amp;#39;t have time to actually attend as many talks as I&amp;#39;d have liked. Even though I was &lt;em&gt;present&lt;/em&gt; for the whole of the Scala Koans session in the PreCompilr on Wednesday, I found myself next to &lt;a href="http://mindviewinc.com/Index.php"&gt;Bruce Eckel&lt;/a&gt;, and ended up chatting with him for most of the time. It would have been a bit of a waste not to, really. (And at least &lt;em&gt;some&lt;/em&gt; of that talking was Scala-related...) I also watched the whole of the &lt;a href="http://www.bradygaster.com/codemash-signalr-talk"&gt;SignalR presentation by Brady Gaster&lt;/a&gt; - where I was apparently the only person in the room with an aversion to &amp;quot;dynamic&amp;quot; in C# 4. That made me the butt of a &lt;em&gt;few&lt;/em&gt; jokes, but not too many for comfort.&lt;/p&gt;  &lt;p&gt;In terms of C#-related talks, I went to the first half of Dustin Campbell&amp;#39;s Roslyn session, but was somewhat distracted by putting together Glee slides and had to leave half way through to hand them in. My final session of the conference was Bill Wagner&amp;#39;s &amp;quot;Stunt coding in C# - I dare you to try this at home&amp;quot; which was excellent, and a very fitting end to the conference for me.&lt;/p&gt;  &lt;p&gt;Highlights of the conference for me:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Messing with Bill Wagner&amp;#39;s code at the end of not just our joint async talk but also his Stunt Coding talk. I&amp;#39;ve never before asked a presented whether they mind me just stealing the keyboard, but I was confident that Bill (and the attendees) would get a kick out of it - and the code &lt;em&gt;was&lt;/em&gt; nicer afterwards :) &lt;/li&gt;    &lt;li&gt;Meeting &lt;em&gt;so&lt;/em&gt; many people... some that I&amp;#39;ve met before (I hadn&amp;#39;t seen &lt;a href="http://www.tedneward.com/"&gt;Ted Neward&lt;/a&gt; since I gate-crashed a party at his house after the MVP 2005 Summit), some I&amp;#39;d met virtually but not physically before (like Bill) and there loads of other folks I&amp;#39;d never known at all before - including &lt;a href="https://twitter.com/#!/coridrew"&gt;Cori Drew&lt;/a&gt;. Cori simply seemed to pop up &lt;em&gt;everywhere -&lt;/em&gt; I swear she had about 20 clones at CodeMash. (She also recorded the video of the Glee talk, and it&amp;#39;s her laughter you can hear - thanks very much, Cori!) Everyone at the conference was incredibly friendly, and I was really touched by how many people said on the last day that they&amp;#39;d appreciated me making the long trip.&lt;/li&gt;    &lt;li&gt;Confounding &lt;a href="https://twitter.com/#!/dcampbell"&gt;Dustin Campbell&lt;/a&gt; and &lt;a href="https://twitter.com/#!/pilchie"&gt;Kevin Pilch-Bisson&lt;/a&gt; with my &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2010/11/02/evil-code-overload-resolution-workaround.aspx"&gt;evil generic overloading puzzle&lt;/a&gt;. Just to be clear, these are two seriously smart guys and this was a friendly over-lunch challenge. It&amp;#39;s always a privilege to meet more of the team responsible for C# and Visual Studio.&lt;/li&gt;    &lt;li&gt;The number of families who came - this is something I&amp;#39;ve never seen at other conferences, and it really made a difference in terms of the atmosphere of the non-dev bits. It was fabulous to see the kids in the water park, for example. Even out of just attendees, there was a greater proportion of women at CodeMash than at other conferences I&amp;#39;ve been to - obviously still vastly outnumbered by the men, but it was nice to see &lt;em&gt;some&lt;/em&gt; improvement on that front. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;This will probably be my only international conference for 2012, so it&amp;#39;s a good job that it was so wonderfully organized. I really hope I have the chance to attend next year too. Many thanks to everyone who helped make it such a special conference.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1804880" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/U1lOnDVXmy4" height="1" width="1"/&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Speaking+engagements/default.aspx">Speaking engagements</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2012/01/15/codemash-2012-report.aspx</feedburner:origLink></item><item><title>Eduasync part 18: Changes between the Async CTP and the Visual Studio 11 Preview</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/G48vdcVNoSQ/eduasync-part-18-changes-between-the-async-ctp-and-the-visual-studio-11-preview.aspx</link><pubDate>Thu, 12 Jan 2012 01:34:31 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1804610</guid><dc:creator>skeet</dc:creator><slash:comments>6</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1804610</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1804610</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2012/01/12/eduasync-part-18-changes-between-the-async-ctp-and-the-visual-studio-11-preview.aspx#comments</comments><description>&lt;p&gt;In preparation for CodeMash, I&amp;#39;ve been writing some more async code and decompiling it with Reflector. This time I&amp;#39;m using the Visual Studio 11 Developer Preview - the version which installs alongside Visual Studio 2010 under Windows 7. (Don&amp;#39;t ask me about any other features of Visual Studio 11 - I haven&amp;#39;t explored it thoroughly; I&amp;#39;ve really only used it for the C# 5 bits.)&lt;/p&gt;  &lt;p&gt;There have been quite a few changes since the CTP - they&amp;#39;re not &lt;em&gt;visible&lt;/em&gt; changes in terms of code that you&amp;#39;d normally write, but the state machine generated by the C# compiler is reasonably different. In this post I&amp;#39;ll describe the differences, as best I understand them. There are still a couple of things I don&amp;#39;t understand (which I&amp;#39;ll highlight within the post) but overall, I think I&amp;#39;ve got a pretty good handle on why the changes have been made.&lt;/p&gt;  &lt;p&gt;I&amp;#39;m going to assume you already have a &lt;em&gt;reasonable&lt;/em&gt; grasp of the basic idea of async and how it works - the way that the compiler generates a state machine to represent an async method or anonymous function, with originally-local variables being promoted to instance variables within the state machine, etc. If the last sentence was a complete mystery to you, see &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2011/05/20/eduasync-part-7-generated-code-from-a-simple-async-method.aspx"&gt;Eduasync part 7&lt;/a&gt; for more information. I don&amp;#39;t expect you to remember the &lt;em&gt;exact&lt;/em&gt; details of what was in the previous CTP though :)&lt;/p&gt;  &lt;h2&gt;Removal of iterator block leftovers&lt;/h2&gt;  &lt;p&gt;In the CTP, the code for async methods was based on the iterator block implementation. I suspect that&amp;#39;s still the case, but possibly sharing just a little less code. There used to be a few methods and fields which weren&amp;#39;t used in async methods, but now they&amp;#39;re gone:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;There&amp;#39;s no now constructor, so no need for the &amp;quot;skeleton&amp;quot; method which replaces the real async method to pass in 0 as the initial state. &lt;/li&gt;    &lt;li&gt;There&amp;#39;s no Dispose method. &lt;/li&gt;    &lt;li&gt;There&amp;#39;s no disposing field. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;It&amp;#39;s nice to see these gone, but it&amp;#39;s not terribly interesting. Now on to the bigger changes...&lt;/p&gt;  &lt;h2&gt;Large structural changes&lt;/h2&gt;  &lt;p&gt;There&amp;#39;s a set of related structural changes which don&amp;#39;t make sense individually. I&amp;#39;ll describe them first, then look at how it all hangs together, and my guess as to the reasoning behind.&lt;/p&gt;  &lt;h3&gt;The state machine is now a struct&lt;/h3&gt;  &lt;p&gt;The declaration of the nested type for the state machine is now something like this:&lt;/p&gt;  &lt;div class="code"&gt;[CompilerGenerated]    &lt;br /&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;struct&lt;/span&gt; StateMachine : IStateMachine     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Fields common to all async state machines&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// (with caveats)&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;int&lt;/span&gt; state;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;object&lt;/span&gt; awaiter;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt; AsyncTaskMethodBuilder&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt; builder;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt; Action moveNextDelegate;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;object&lt;/span&gt; stack;     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Hoisted local variables&lt;/span&gt;     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Methods&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; [DebuggerHidden]     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; SetMoveNextDelegate(Action action) { ... }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; MoveNext() { ... }     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;The caveats around the common field are in terms of the return type of the async method (which determines the type of builder used) and whether or not there are any awaits (if there are no awaits, the stack and awaiter fields aren&amp;#39;t generated).&lt;/p&gt;  &lt;p&gt;Note that throughout this blog post I&amp;#39;ve changed the names of fields and types - in reality they&amp;#39;re all &amp;quot;unspeakable&amp;quot; names including angle-brackets, just like all compiler-generated names.&lt;/p&gt;  &lt;h3&gt;There&amp;#39;s a new assembly-wide interface&lt;/h3&gt;  &lt;p&gt;As you can see from the code above, the state machine implements an interface (actually called &amp;lt;&amp;gt;t__IStateMachine). One of these is created in the global namespace in each assembly that contains at least one async method or anonymous function, and it looks like this:&lt;/p&gt;  &lt;div class="code"&gt;[CompilerGenerated]    &lt;br /&gt;&lt;span class="Modifier"&gt;internal&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;interface&lt;/span&gt; IStateMachine     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ValueType"&gt;void&lt;/span&gt; SetMoveNextDelegate(Action action);     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;The implementation for this method is always the same, and it&amp;#39;s trivial:&lt;/p&gt;  &lt;div class="code"&gt;[DebuggerHidden]    &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; SetMoveNextDelegate(Action action)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Keyword"&gt;this&lt;/span&gt;.moveNextDelegate = action;     &lt;br /&gt;} &lt;/div&gt;  &lt;h3&gt;Simplified skeleton method&lt;/h3&gt;  &lt;p&gt;The method which starts the state machine, which I&amp;#39;ve been calling the &amp;quot;skeleton&amp;quot; method everywhere, is now a bit simpler than it was. Something like this:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt; Task&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt; FooAsync()     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; StateMachine machine = &lt;span class="Keyword"&gt;new&lt;/span&gt; StateMachine();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; machine.builder = AsyncVoidMethodBuilder.Create();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; machine.MoveNext();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; machine.builder.Task;     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;In fact if you decompile the IL, you&amp;#39;ll see that it &lt;em&gt;doesn&amp;#39;t&lt;/em&gt; explicitly initialize the variable to start with - it just declares it, sets the builder field and then calls MoveNext(). That&amp;#39;s not valid C# (as all the struct&amp;#39;s fields aren&amp;#39;t initialized), but it &lt;em&gt;is&lt;/em&gt; valid IL. It&amp;#39;s &lt;em&gt;equivalent&lt;/em&gt; to the code above though. Note how there&amp;#39;s nothing to set the continuation - where previously the moveNextDelegate field would be populated within the skeleton method.&lt;/p&gt;  &lt;h3&gt;Just-in-time delegate creation&lt;/h3&gt;  &lt;p&gt;Now that the skeleton method doesn&amp;#39;t create the delegate representing the continuation, it can be done when it&amp;#39;s first required - which is when we first encounter an await expression for an awaitable which hasn&amp;#39;t already completed. (If the awaitable has completed before we await it, the generated code skips the continuation and just uses the results immediately and synchronously).&lt;/p&gt;  &lt;p&gt;The code for that delegate creation is slightly trickier than you might expect, however. It looks something like this:&lt;/p&gt;  &lt;div class="code"&gt;Action action = &lt;span class="Keyword"&gt;this&lt;/span&gt;.moveNextDelegate;     &lt;br /&gt;&lt;span class="Statement"&gt;if&lt;/span&gt; (action == &lt;span class="Keyword"&gt;null&lt;/span&gt;)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Task&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt; task = &lt;span class="Keyword"&gt;this&lt;/span&gt;.builder.Task;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; action = &lt;span class="Keyword"&gt;new&lt;/span&gt; Action(&lt;span class="Keyword"&gt;this&lt;/span&gt;.MoveNext);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ((IStateMachine) action.Target).SetMoveNextDelegate(action);     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;There are two oddities here, one of which I mostly understand and one of which I don&amp;#39;t understand at all.&lt;/p&gt;  &lt;p&gt;I really don&amp;#39;t understand the &amp;quot;task&amp;quot; variable here. Why do we need to exercise the AsyncTaskMethodBuilder.Task property? We don&amp;#39;t use the result anywhere... does forcing this flush some memory buffer? I have no clue on this one. &lt;strong&gt;(See the update at the bottom of the post...)&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The part about setting the delegate via the interface makes more sense, but it&amp;#39;s subtle. You might &lt;em&gt;expect&lt;/em&gt; code like this:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="InlineComment"&gt;// Looks sensible, but is actually slightly broken&lt;/span&gt;     &lt;br /&gt;Action action = &lt;span class="Keyword"&gt;this&lt;/span&gt;.moveNextDelegate;     &lt;br /&gt;&lt;span class="Statement"&gt;if&lt;/span&gt; (action == &lt;span class="Keyword"&gt;null&lt;/span&gt;)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; action = &lt;span class="Keyword"&gt;new&lt;/span&gt; Action(&lt;span class="Keyword"&gt;this&lt;/span&gt;.MoveNext);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Keyword"&gt;this&lt;/span&gt;.moveNextDelegate = action;     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;That would &lt;em&gt;sort of&lt;/em&gt; work - but we&amp;#39;d end up needing to recreate the delegate each time we encountered an appropriate await expression. Although the above code saves the value to the field, it saves it within the current value of the state machine... &lt;em&gt;after&lt;/em&gt; we&amp;#39;ve boxed that value as the target of the delegate. The value we want to mutate is the one &lt;em&gt;within the box&lt;/em&gt; - which is precisely why there&amp;#39;s an interface, and why the code casts to it.&lt;/p&gt;  &lt;p&gt;We can&amp;#39;t even just unbox and then set the field afterwards - at least in C# - because the unbox operation is always followed by a copy operation in normal C#. I &lt;em&gt;believe&lt;/em&gt; it would be possible for the C# compiler to generate IL which unboxed action.Target &lt;em&gt;without&lt;/em&gt; the copy, and then set the field in that. It&amp;#39;s not clear to me why the team went with the interface approach instead... I would &lt;em&gt;expect&lt;/em&gt; that to be slower (as it requires dynamic dispatch) but I could easily be wrong. Of course, it would also make it impossible to decompile the IL to C#, which would make my talks harder, but don&amp;#39;t expect the C# team to bend the compiler implementation for my benefit ;)&lt;/p&gt;  &lt;p&gt;(As an aside to all of this, I&amp;#39;ve gone back and forth on whether the &amp;quot;slightly broken&amp;quot; implementation would recreate the delegate on &lt;em&gt;every&lt;/em&gt; appropriate await, or only two. I &lt;em&gt;think&lt;/em&gt; it would end up being on every occurrence, as even though on the second occurrence we&amp;#39;d be operating within the context of the first boxed instance, the new delegate would have a reference to a &lt;em&gt;new&lt;/em&gt; boxed copy each time. It does my head in a little bit, trying to think about this... more evidence that mutable structs are evil and hard to reason about. It&amp;#39;s not the wrong decision in this case, hidden far from the gaze of normal developers, but it&amp;#39;s a pain to reason about.)&lt;/p&gt;  &lt;h3&gt;Single awaiter variable&lt;/h3&gt;  &lt;p&gt;In the CTP, each await expression generated a separate field within the state machine, and that field was always of the exact awaiter type. In the VS11 Developer Preview, there&amp;#39;s always exactly &lt;em&gt;one&lt;/em&gt; awaiter field (assuming there&amp;#39;s at least one await expression) and it&amp;#39;s always of type object. It&amp;#39;s used like this:&lt;/p&gt;  &lt;div class="code"&gt;&amp;#160; &lt;span class="InlineComment"&gt;// Single local variable used by both continuation and first-time paths&lt;/span&gt;     &lt;br /&gt;&amp;#160; TaskAwaiter&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt; localAwaiter;     &lt;br /&gt;    &lt;br /&gt;&amp;#160; ...     &lt;br /&gt;    &lt;br /&gt;&amp;#160; &lt;span class="Statement"&gt;if&lt;/span&gt; (conditions-&lt;span class="Statement"&gt;for&lt;/span&gt;-first-time-execution)     &lt;br /&gt;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Code before await&lt;/span&gt;     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; localAwaiter = task.GetAwaiter();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;if&lt;/span&gt; (localAwaiter.IsCompleted)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;goto&lt;/span&gt; Await1Completed;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Keyword"&gt;this&lt;/span&gt;.state = 1;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; TaskAwaiter&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt;[] awaiterArray = { localAwaiter };     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Keyword"&gt;this&lt;/span&gt;.awaiter = awaiterArray;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Lazy delegate creation goes here&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; awaiterArray[0].OnCompleted(action);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt;;     &lt;br /&gt;&amp;#160; }     &lt;br /&gt;&amp;#160; &lt;span class="InlineComment"&gt;// Continuation would get into here&lt;/span&gt;     &lt;br /&gt;&amp;#160; localAwaiter = ((TaskAwaiter&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt;[]) &lt;span class="Keyword"&gt;this&lt;/span&gt;.awaiter)[0];     &lt;br /&gt;&amp;#160; &lt;span class="Keyword"&gt;this&lt;/span&gt;.awaiter = &lt;span class="Keyword"&gt;null&lt;/span&gt;;     &lt;br /&gt;&amp;#160; &lt;span class="Keyword"&gt;this&lt;/span&gt;.state = 0;     &lt;br /&gt;Await1Completed:     &lt;br /&gt;&amp;#160; &lt;span class="ValueType"&gt;int&lt;/span&gt; result = localAwaiter.GetResult();&amp;#160; &lt;br /&gt;&amp;#160; localA&lt;span class="ValueType"&gt;&lt;font color="#333333"&gt;waiter = &lt;/font&gt;&lt;span class="Modifier"&gt;default&lt;/span&gt;&lt;font color="#333333"&gt;(TaskAwaiter&amp;lt;&lt;/font&gt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&lt;font color="#333333"&gt;&amp;gt;);&lt;/font&gt;&lt;/span&gt; &lt;/div&gt;  &lt;p&gt;I realize there&amp;#39;s a lot of code here, but it does make some sense:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The value of the awaiter field is always either null, or a reference to a single-element array of the awaiter type for one of the await expressions. &lt;/li&gt;    &lt;li&gt;A single localAwaiter variable is shared between the two code paths, populated either from the awaitable (on the initial code path) or by copying the value from the array (in the second code path). &lt;/li&gt;    &lt;li&gt;The field is always set to null and the local variable is set to its default value after use, presumably for the sake of garbage collection &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;It&amp;#39;s basically a nice way of using the fact that we&amp;#39;ll only ever need one awaiter at a time. It&amp;#39;s not clear to me why an array is used instead of either using a reference to the awaiter for class-based awaiters, or simply by boxing for struct-based awaiters. The latter would need the same &amp;quot;unbox without copy&amp;quot; approach discussed in the previous section - so if there&amp;#39;s some reason why that&amp;#39;s actually infeasible, it would explain the use of an array here. We &lt;em&gt;can&amp;#39;t&lt;/em&gt; use the interface trick in this case, as the compiler isn&amp;#39;t in control of the awaiter type (so can&amp;#39;t make it implement an interface).&lt;/p&gt;  &lt;h3&gt;Expression stack preservation&lt;/h3&gt;  &lt;p&gt;This one is actually a fix to a bug in the async CTP, which I&amp;#39;ve &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2011/06/10/eduasync-part-10-ctp-bug-don-t-combine-multiple-awaits-in-one-statement.aspx"&gt;written about before&lt;/a&gt;. We&amp;#39;re used to the stack containing our local variables (in the absence of iterator blocks, captured variables etc, and modulo the stack being an implementation detail) but it&amp;#39;s also used for intermediate results within a single statement. For example, consider this block of code:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="ValueType"&gt;int&lt;/span&gt; x = 10;     &lt;br /&gt;&lt;span class="ValueType"&gt;int&lt;/span&gt; y = 5;     &lt;br /&gt;&lt;span class="ValueType"&gt;int&lt;/span&gt; z = x + 50 * y; &lt;/div&gt;  &lt;p&gt;That last line is effectively:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Load the value of x onto the stack &lt;/li&gt;    &lt;li&gt;Load the value 50 onto the stack &lt;/li&gt;    &lt;li&gt;Load the value of y onto the stack &lt;/li&gt;    &lt;li&gt;Multiply the top two stack values (50 and y) leaving the result on the stack &lt;/li&gt;    &lt;li&gt;Add the top two stack values (x and the previously-computed result) leaving the result on the stack &lt;/li&gt;    &lt;li&gt;Store the top stack value into z &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Now suppose we want to turn y into a Task&amp;lt;int&amp;gt;:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="ValueType"&gt;int&lt;/span&gt; x = 10;     &lt;br /&gt;Task&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt; y = Task.FromResult(5);     &lt;br /&gt;&lt;span class="ValueType"&gt;int&lt;/span&gt; z = x + 50 * &lt;span class="Modifier"&gt;await&lt;/span&gt; y; &lt;/div&gt;  &lt;p&gt;Our state machine needs to make sure that it will preserve the same behaviour as the synchronous version, so it needs the same sort of stack. In the new-style state machine, all of that stack is saved in the &amp;quot;stack&amp;quot; field. It&amp;#39;s only one field, but may need to represent multiple different types within the code at various different await expressions - in the code above, for example, it represents two int values. As far as I can discover, the C# compiler generates code which uses the actual type of the value it needs, if it only requires a single value. If it needs multiple values, it uses an appropriate Tuple type, nesting tuples if it goes beyond the number of type parameters supported by the Tuple&amp;lt;...&amp;gt; family of types. So in our case above, we end up with code a &lt;em&gt;bit&lt;/em&gt; like this:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="InlineComment"&gt;// Local variable used in both paths&lt;/span&gt;     &lt;br /&gt;Tuple&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;, &lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt; tuple;     &lt;br /&gt;    &lt;br /&gt;...     &lt;br /&gt;    &lt;br /&gt;&lt;span class="InlineComment"&gt;// Code before the await&lt;/span&gt;     &lt;br /&gt;&lt;span class="Statement"&gt;if&lt;/span&gt; (conditions-&lt;span class="Statement"&gt;for&lt;/span&gt;-first-time-execution)&amp;#160; &lt;br /&gt;{&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ...     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; tuple = &lt;span class="Keyword"&gt;new&lt;/span&gt; Tuple&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;, &lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt;(&lt;span class="Keyword"&gt;this&lt;/span&gt;.x, 50);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Keyword"&gt;this&lt;/span&gt;.stack = tuple;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ...     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;&lt;span class="InlineComment"&gt;// Continuation would get into here&lt;/span&gt;     &lt;br /&gt;tuple = (Tuple&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;, &lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt;) &lt;span class="Keyword"&gt;this&lt;/span&gt;.stack;     &lt;br /&gt;&lt;span class="InlineComment"&gt;// IL copies the values from the tuple onto the stack at this point&lt;/span&gt;     &lt;br /&gt;&lt;span class="Keyword"&gt;this&lt;/span&gt;.stack = &lt;span class="Keyword"&gt;null&lt;/span&gt;;     &lt;br /&gt;    &lt;br /&gt;...     &lt;br /&gt;    &lt;br /&gt;&lt;span class="InlineComment"&gt;// Both the fast and slow code paths get here eventually&lt;/span&gt;     &lt;br /&gt;&lt;span class="Keyword"&gt;this&lt;/span&gt;.z = stack0 + stack1 * awaiter.GetResult() &lt;/div&gt;  &lt;p&gt;I say it&amp;#39;s a &lt;em&gt;bit&lt;/em&gt; like this, because it&amp;#39;s hard to represent the IL exactly in C# in this case. The tuple is only created if it&amp;#39;s needed, i.e. not in the already-completed fast path. In that case, the values are loaded onto the stack but &lt;em&gt;not&lt;/em&gt; then put into the tuple - execution skips straight to the code which uses the values already on the stack.&lt;/p&gt;  &lt;p&gt;When the awaitable &lt;em&gt;isn&amp;#39;t&lt;/em&gt; complete immediately, then a Tuple&amp;lt;int, int&amp;gt; is created, stored in the &amp;quot;stack&amp;quot; field, and the continuation is handed to the awaiter. On continuation, the tuple is loaded back from the &amp;quot;stack&amp;quot; field (and cast accordingly), the values are loaded onto the stack - and then we&amp;#39;re back into the common code path of fetching the value and performing the add and multiply operations.&lt;/p&gt;  &lt;h2&gt;Conclusion&lt;/h2&gt;  &lt;p&gt;As far as I&amp;#39;m aware, those are the most noticeable changes in the generated code. There may well still be a load more changes in Task&amp;lt;T&amp;gt; and the TPL in general - I wouldn&amp;#39;t be at all surprised - but that&amp;#39;s harder to investigate.&lt;/p&gt;  &lt;p&gt;I&amp;#39;m sure all of this has been done in the name of performance (and correctness, in the case of stack preservation). The state machine is now much smaller in terms of the number of fields it requires, and objects are created locally as far as possible (including the state machine itself only requiring heap allocation if there&amp;#39;s ever a &amp;quot;slow&amp;quot; awaitable). I suspect there&amp;#39;s still some room for optimization, however:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Both the awaiter and the delegate use careful boxing and either arrays or a mutating interface to allow the boxed value to be changed. I suspect that using unbox with the concrete type, but without copying the value, would be more efficient. I may attempt to work this theory up into a test at some point. &lt;/li&gt;    &lt;li&gt;If there&amp;#39;s only one awaiter type (usually TaskAwaiter&amp;lt;T&amp;gt; for some T), that type could be used instead of object, potentially reducing heap optimization &lt;/li&gt;    &lt;li&gt;I&amp;#39;ve no idea why the builder.Task property is explicitly fetched and then the results discarded&lt;/li&gt;    &lt;li&gt;If there&amp;#39;s only one await expression, the &amp;quot;stack&amp;quot; field can be strongly typed, which would also avoid boxing if only a single value needs to be within that stack &lt;/li&gt;    &lt;li&gt;The stack field could be removed entirely when it&amp;#39;s not needed for intermediate stack value preservation. (I believe that would be the case reasonably often.) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The use of mutable value types is really fascinating (for me, at least) - I&amp;#39;m sure most people on the C# team would still say they&amp;#39;re evil, but when they&amp;#39;re used in a carefully controlled environment where real developers don&amp;#39;t have to reason about their behaviour, they can be useful.&lt;/p&gt;  &lt;p&gt;Next time, I&amp;#39;ll hopefully get back to the idea I promised to write up before, about ordering a collection of tasks in completion order... before they&amp;#39;ve completed. (Well, sort of.) Should be fun...&lt;/p&gt;  &lt;h2&gt;Update (January 16th 2012)&lt;/h2&gt;  &lt;p&gt;Stephen Toub got in touch with me after I posted the original version of this blog entry, to explain the use of the Task property. Apparently the idea is that at this point, we &lt;em&gt;know&lt;/em&gt; we&amp;#39;re going to return out of the state machine, so the skeleton method is going to access the Task property anyway. However, as we haven&amp;#39;t scheduled the continuation yet we &lt;em&gt;also&lt;/em&gt; know that nothing will be accessing the Task property on a different thread. If we access it now for the first time, we can lazily allocate the task &lt;em&gt;in the same thread that created the AsyncMethodBuilder, with no risk of contention.&lt;/em&gt; If we can force there to be a task ready and waiting for whatever accesses it later, we don&amp;#39;t need any synchronization in that area.&lt;/p&gt;  &lt;p&gt;So why might we want to allocate the task lazily in the first place? Well, don&amp;#39;t forget that we might &lt;em&gt;never&lt;/em&gt; have to wait for an await (as it were). We might just have an async method which takes the fast path everywhere. If that&amp;#39;s the case, then for certain cases (e.g. a non-generic, successfully completed task, or a Task&amp;lt;bool&amp;gt; which again has completed successfully) we can reuse the same instance repeatedly. Apparently this laziness isn&amp;#39;t yet part of the VS11 Developer Preview, but the reason for the property access is in preparation for this.&lt;/p&gt;  &lt;p&gt;Another case of micro-optimization - which is fair enough when it&amp;#39;s at a system level :)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1804610" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/G48vdcVNoSQ" height="1" width="1"/&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+5/default.aspx">C# 5</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/async/default.aspx">async</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Eduasync/default.aspx">Eduasync</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2012/01/12/eduasync-part-18-changes-between-the-async-ctp-and-the-visual-studio-11-preview.aspx</feedburner:origLink></item><item><title>Awaiting CodeMash 2012</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/7y3aScdTwxA/awaiting-codemash-2012.aspx</link><pubDate>Sun, 01 Jan 2012 20:43:10 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1804280</guid><dc:creator>skeet</dc:creator><slash:comments>2</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1804280</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1804280</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2012/01/01/awaiting-codemash-2012.aspx#comments</comments><description>&lt;p&gt;Happy New Year, everyone!&lt;/p&gt;  &lt;p&gt;I&amp;#39;m attempting to make 2012 a quiet year in terms of my speaking engagements - I&amp;#39;ve turned down a few kind offers already, and I expect to do so again during the year. I may well still give user group talks in evenings if I can do so without having to take holiday, but full conferences are likely to be out, especially international ones. This is partly so I can take more time off to support my wife, Holly, who has &lt;a href="http://www.amazon.co.uk/s/ref=sr_tc_2_0?rh=i%3Astripbooks%2Ck%3AHolly+Webb&amp;amp;field-contributor_id=B0034Q363U"&gt;her own books to promote&lt;/a&gt;. This year will be particularly important for Holly as she&amp;#39;s one of the &lt;a href="http://www.worldbookday.com/"&gt;World Book Day 2012&lt;/a&gt; authors - I&amp;#39;m &lt;em&gt;tremendously&lt;/em&gt; proud of her, as you can no doubt imagine.&lt;/p&gt;  &lt;p&gt;However, there&amp;#39;s one international conference I decided to submit proposals for: &lt;a href="http://codemash.org/"&gt;CodeMash&lt;/a&gt;. I&amp;#39;ve never been to this or any other US conference, but I&amp;#39;ve heard fabulous things about it. I&amp;#39;m particularly excited that I&amp;#39;ll be able to present alongside Bill Wagner, a fellow C# author (probably most famous for &lt;a href="http://www.amazon.com/dp/0321245660"&gt;Effective C#&lt;/a&gt; which I&amp;#39;ve &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2010/09/25/book-review-effective-c-2nd-edition-by-bill-wagner.aspx"&gt;reviewed before now&lt;/a&gt;). Bill and I have never met, although we&amp;#39;ve participated jointly on a &lt;a href="http://www.dotnetrocks.com/default.aspx?showNum=611"&gt;.NET Rocks show&lt;/a&gt; before now. I could barely hear Bill when we recorded that though, so it hardly counts :)&lt;/p&gt;  &lt;p&gt;The conference schedule for CodeMash shows Bill and I each giving two talks: two individual ones on general C# (&lt;a href="http://codemash.org/Sessions#C%23+Stunt+Coding%3a+I+Dare+You+to+Try+This+at+Home"&gt;C# Stunt Coding&lt;/a&gt; by Bill, and &lt;a href="http://codemash.org/Sessions#C%23&amp;#39;s+Greatest+Mistakes"&gt;C#&amp;#39;s Greatest Mistakes&lt;/a&gt; by me) and two sessions on the async support in C# 5... async &amp;quot;from the inside&amp;quot; and &amp;quot;from the outside&amp;quot;. Although these have hitherto been shown as separate sessions, everyone involved thought it would make more sense to weave the two together... so this will be a double-length session. Bill will be presenting the &amp;quot;outside&amp;quot; view - how to &lt;em&gt;use&lt;/em&gt; async, basically; I&amp;#39;ll be presenting the &amp;quot;inside&amp;quot; view - how it all hangs together behind the scenes.&lt;/p&gt;  &lt;p&gt;With any luck, this will be much more helpful to the conference attendees, as they should be able to build up confidence in the solid foundations underpinning it all at the same time as seeing how fabulously useful it&amp;#39;ll be for developers. It also means that Bill and I can bounce ideas off each other spontaneously as we go - I intend to pay close attention and learn a thing or two myself!&lt;/p&gt;  &lt;p&gt;It&amp;#39;s pretty much impossible to predict how it&amp;#39;ll all hang together, but I&amp;#39;m really excited about the whole shebang. I&amp;#39;ll be fascinated to see if and how US conferences differ from the various ones this side of the pond... but it does make the whole thing that bit more nerve-wracking. If you&amp;#39;re coming to CodeMash, please grab me and say hi - it never hurts to see a friendly face...&lt;/p&gt;  &lt;p&gt;(Note: Bill has a &lt;a href="http://billwagner.cloudapp.net/Home/Item/LookingforwardtoCodeMashInsideandOutside"&gt;similar blog post&lt;/a&gt; posted just before this one.)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1804280" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/7y3aScdTwxA" height="1" width="1"/&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Speaking+engagements/default.aspx">Speaking engagements</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2012/01/01/awaiting-codemash-2012.aspx</feedburner:origLink></item><item><title>Book Review: Fluent C# (Rebecca Riordan, Sams)</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/wg2DJGKWqNY/book-review-fluent-c-rebecca-riordan-sams.aspx</link><pubDate>Mon, 05 Dec 2011 19:26:25 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1803256</guid><dc:creator>skeet</dc:creator><slash:comments>28</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1803256</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1803256</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2011/12/05/book-review-fluent-c-rebecca-riordan-sams.aspx#comments</comments><description>&lt;p&gt;(As usual, I will be sending the publisher a copy of this review to give them and the author a chance to reply to it before I publish it to the blog. Other than including their comments and correcting any factual mistakes they may point out, I don&amp;#39;t intend to change the review itself.)&lt;/p&gt;  &lt;h3&gt;Resources:&lt;/h3&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://www.informit.com/store/product.aspx?isbn=0672331047"&gt;Publisher page (includes source code download)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.amazon.com/dp/0672331047"&gt;Amazon&lt;/a&gt; / &lt;a href="http://www.barnesandnoble.com/w/fluent-c-rebecca-m-riordan/1100388197?ean=9780672331046&amp;amp;itm=1&amp;amp;usri=fluent+c%23"&gt;Barnes and Noble&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="https://docs.google.com/document/d/1GNZ77fBu15gPnpmelcujlJVdc_iEvJWjLMd0m2VgStA/edit?hl=en_GB"&gt;My unofficial errata and notes&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://kellyschronicles.wordpress.com/category/fluent/"&gt;A more positive series of review blog posts&lt;/a&gt; (just for balance) &lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Introduction and disclaimers&lt;/h3&gt;  &lt;p&gt;In late October, Sams (the publisher) approached me to ask if I&amp;#39;d be interested in reviewing their newest introductory book on C#. Despite my burgeoning review stack, I said I was interested - I&amp;#39;m always on the lookout for good books to recommend. So, the first disclaimer is that this was a review copy - I didn&amp;#39;t have to pay for it. I don&amp;#39;t believe that has biased this review though.&lt;/p&gt;  &lt;p&gt;Second disclaimer: obviously as C# in Depth is also &amp;quot;a book about C#&amp;quot; you might be wondering whether the two books are competitors. I don&amp;#39;t believe this is the case: Fluent C# explicitly talks about its target audience, which is primarily complete newcomers to programming. C# in Depth pretty much requires you to know at least C# 1, or perhaps be very comfortable with a similar language such as Java. I find it hard to imagine someone for whom both books would be suitable.&lt;/p&gt;  &lt;p&gt;Obviously that puts me firmly &lt;em&gt;out&lt;/em&gt; of the target audience. &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2008/07/08/the-trouble-with-book-reviews.aspx"&gt;As I&amp;#39;ve written before&lt;/a&gt;, if you think the two most important questions to answer in a technical book review are &amp;quot;Is it accurate?&amp;quot; and &amp;quot;How good is at teaching its topic?&amp;quot; then any one person will find it hard to answer both questions. Although I&amp;#39;m far from an expert in some of the areas of the book - notably WPF - I&amp;#39;m sure I don&amp;#39;t have the same approach as a true newcomer. In particular, I find myself asking the questions I&amp;#39;d need the answers to in order to develop software professionally: how do I test it? How does the deployment model work? How does the data flow? These aren&amp;#39;t the same concerns as someone who is coming to programming for the first time. This review should be read with that context in mind: that my approach to the subject matter won&amp;#39;t be the same as a regular reader&amp;#39;s.&lt;/p&gt;  &lt;h3&gt;Physical format and style&lt;/h3&gt;  &lt;p&gt;Fluent C# is &lt;em&gt;very&lt;/em&gt; reminiscent of Head-First C# in its approach, even down to the introductory &amp;quot;why this book is great at teaching you&amp;quot; blurb. It&amp;#39;s all very informal, with lots of pictures, diagrams and reader exercises. It&amp;#39;s a chunky book, at nearly 900 pages including the index - which I&amp;#39;d expect to be pretty daunting to a newcomer. However, that isn&amp;#39;t the main impression you come away with. Instead...&lt;/p&gt;  &lt;p&gt;It&amp;#39;s brown. &lt;strong&gt;Everywhere&lt;/strong&gt;. The diagrams, the text, the pictures - they&amp;#39;re all printed in brown, on off-white paper.&lt;/p&gt;  &lt;p&gt;Combined with using multiple fonts including cursive ones, this makes for a pointlessly irritating reading experience right from the outset, however good or bad the actual content is. Now it&amp;#39;s &lt;em&gt;possible&lt;/em&gt; that this is actually deliberate: I was speaking to someone recently who mentioned some research that shows if you use a hard-to-read font in presentations, people tend to end up reading it several times, so you end up with &lt;em&gt;better&lt;/em&gt; memories of the content than if it had been &amp;quot;clean&amp;quot;. I don&amp;#39;t know if that&amp;#39;s what Sams intended with this book, but I frequently found myself longing for simple black ink on clean white paper.&lt;/p&gt;  &lt;p&gt;Leaving that to one side, I&amp;#39;m not sure I&amp;#39;ll ever &lt;em&gt;really&lt;/em&gt; be a fan of the general tone of books like this, but I can certainly see that it&amp;#39;s popular and therefore presumably helpful to many people. It&amp;#39;s not clear to me whether it&amp;#39;s possible to create a book which retains the valuable elements of this style while casting off the aspects which rub me up the wrong way. It&amp;#39;s something about the enforced jollity which just doesn&amp;#39;t quite sit right, but it wouldn&amp;#39;t surprise me if that were more a peculiarity of my personality than anything about the book. Again, I&amp;#39;ve tried to set this to one side when reviewing the book, but it may come through nonetheless.&lt;/p&gt;  &lt;h3&gt;Structure&lt;/h3&gt;  &lt;p&gt;The book is broken up into the following sections, with several chapters per section:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Getting started (122 pages - finding your way around Visual Studio, debugging, deployment) &lt;/li&gt;    &lt;li&gt;The Language (100 pages - introduction to C#) &lt;/li&gt;    &lt;li&gt;The .NET Framework Library (162 pages - text, date/time APIs, collections - and actually more about C# as a language) &lt;/li&gt;    &lt;li&gt;Best practice (116 pages - inheritance, some principles, design patterns) &lt;/li&gt;    &lt;li&gt;WPF (341 pages) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I&amp;#39;ve included the page count for each section to show just how much is devoted to WPF. The book goes into much more detail about WPF than it does about the C# language itself (for example, drop shadow effects are included, but the &amp;quot;using&amp;quot; statement and nullable value types aren&amp;#39;t). If you want to write any kind of application other than a WPF one, a large part of the book won&amp;#39;t be useful to you. That&amp;#39;s not to say it&amp;#39;s useless per se - and in fact from my point of view, the WPF section was the most useful. The section on brushes is probably the best written in the whole book, for example. At time it &lt;em&gt;feels&lt;/em&gt; to me like the author really wanted to write a book about WPF, but was asked to make it one about C# instead. That may well not be the case at all - it was just an impression.&lt;/p&gt;  &lt;p&gt;Even though the best practice section talks briefly about MVC, MVP and MVVM, it doesn&amp;#39;t really go into enough detail to build anything like a real application - and in fact there&amp;#39;s no coverage of persistence of any form. No files, no XML, no database - nothing below the presentation layer, really. As such, although the book &lt;em&gt;claims&lt;/em&gt; it&amp;#39;s enough to get you started with application development, it actually only provides a veneer. Even though I &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2008/03/21/book-review-head-first-c.aspx"&gt;didn&amp;#39;t like the first edition of Head-First C#&lt;/a&gt; back in 2008, it did at least take the reader end-to-end - the exercises led to complete applications. The best practice section isn&amp;#39;t entirely about architecture and design patterns, however - it&amp;#39;s at this point that inheritance is properly introduced. While I wouldn&amp;#39;t personally count that as a &amp;quot;best practice&amp;quot; as such, it does at least come at the start of the section, before the genuine patterns/architecture areas which would have been harder to understand without that background.&lt;/p&gt;  &lt;p&gt;One aspect which concerned me was the emphasis on the debugger and interactive diagnostics. The author states that developers should expect to spend a large part of their time in the debugger, and she says how she prefers using MessageBox.Show for diagnostics over Console.WriteLine information appearing in the output window. While I&amp;#39;m all for something more sophisticated than Console.WriteLine, there are solutions which are a lot less invasive than popping up a dialog, and which can be left in the code (possibly under an execution-time configuration) to allow diagnostics to be produced for &lt;em&gt;real&lt;/em&gt; systems.&lt;/p&gt;  &lt;p&gt;The &amp;quot;testing and deployment&amp;quot; chapter says nothing about &lt;em&gt;automated&lt;/em&gt; tests - it&amp;#39;s as if the author believes that &amp;quot;testing&amp;quot; only involves &amp;quot;running the app in the debugger and seeing if it breaks&amp;quot;. I hope that&amp;#39;s not actually the case, and I can understand why newcomers ought to at least know about the debugger - but I&amp;#39;d have welcomed at least a few pages &lt;em&gt;introducing&lt;/em&gt; unit testing as a way of recording and checking expectations of how your code behaves. My own preference is to spend as little time in the debugger as possible; I know that&amp;#39;s not always practical, particularly for UI work, but I think it&amp;#39;s a reasonable aim.&lt;/p&gt;  &lt;h3&gt;Accuracy&lt;/h3&gt;  &lt;p&gt;Anyone following me on Twitter or Google+ knows where I&amp;#39;m going with this section. After reading through the book, pen in hand (as I always do, even for the books I like), I decided that it was more important to get out some form of errata quickly than this review. As such, I started a &lt;a href="https://docs.google.com/document/d/1GNZ77fBu15gPnpmelcujlJVdc_iEvJWjLMd0m2VgStA/edit?hl=en_GB"&gt;Google document&lt;/a&gt; which is publicly available to read and add comments to. The result is over 60 pages of notes and errata, and that&amp;#39;s excluding the introduction and table of contents. To be fair to the book, some of those notes are matters of disagreement which are more personal opinion than incontrovertible fact - but there are plenty of simple matters of inaccuracy. Some of the worst are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Claims that String is a value type. (It&amp;#39;s a reference type.) &lt;/li&gt;    &lt;li&gt;Inconsistency between whether arrays are value types or reference types - but consistently claiming that arrays are immutable, with the exception of the size which can be changed (slowly) using Array.Resize. (Array types are always reference types, and they&amp;#39;re always mutable &lt;em&gt;except&lt;/em&gt; the size, which is fixed after creation. Array.Resize creates a &lt;em&gt;new&lt;/em&gt; array, it doesn&amp;#39;t change the size of the existing one.) &lt;/li&gt;    &lt;li&gt;Incorrect syntax for chaining from one constructor to another. &lt;/li&gt;    &lt;li&gt;The claim that &lt;em&gt;all&lt;/em&gt; reference types are mutable. (Some aren&amp;#39;t, and indeed I often aim for immutability. The canonical example of an immutable reference type is String.) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;There are plenty more - including huge number of samples which simply won&amp;#39;t compile. Whole double page spreads where every class declaration is missing the &amp;quot;class&amp;quot; keyword. Pieces of code using VB syntax... the list goes on. (The VB syntax errors are probably explained by the author&amp;#39;s other book published at the same time: &amp;quot;Fluent Visual Basic&amp;quot;. I suspect there was a certain amount of copy/paste, and the editing process didn&amp;#39;t catch all the changes which were needed to reflect the differences between the languages.)&lt;/p&gt;  &lt;p&gt;Beyond the factually incorrect statements, there&amp;#39;s the matter of terminology. Now I&amp;#39;m well aware that I care more about terminology than more people - but there&amp;#39;s simply no reason to start making up terminology or misusing the perfectly good terminology from the specification. The book has a whole section on &amp;quot;commands&amp;quot; in C#, including things like for statements, switch statements, try/catch/finally statements. Additionally, it mislabels class and namespace declarations as &amp;quot;statements&amp;quot;, and even mislabels using directives as statements - although it later goes back on the latter point. The word &amp;quot;object&amp;quot; is used at various times to mean any of variable, type, class and object, with no sense of consistency that I could fathom. For example, at one point it&amp;#39;s used in two different senses within the same sentence: &amp;quot;As we&amp;#39;ll see, you can define several different kinds of objects (called TYPES) in C#, but the one you&amp;#39;ll probably work with most often is the OBJECT.&amp;quot;&lt;/p&gt;  &lt;p&gt;Both accuracy and staying consistent with accepted terminology (primarily the specification) are &lt;em&gt;particularly&lt;/em&gt; important for newcomers. If there&amp;#39;s a typo in a relatively advanced book - or in one which is about a particular technology (e.g. MVC) rather than an introductory text on a language, the reader is fairly likely to be able to guess what should really be there based on their existing experience. If a beginner comes across the same problem, they&amp;#39;re likely to assume it&amp;#39;s &lt;em&gt;their&lt;/em&gt; fault that the code won&amp;#39;t compile. Likewise if they learn the wrong terminology to start with, they&amp;#39;ll be severely hampered in communicating effectively with other developers - as well as when reading other books.&lt;/p&gt;  &lt;p&gt;I don&amp;#39;t want to make it sound like I expect perfection in a book - just yesterday someone mailed me a correction to C# in Depth, and I&amp;#39;d be foolish to try to hold other authors to standards I couldn&amp;#39;t meet myself. Nor am I suggesting it&amp;#39;s &lt;em&gt;easy&lt;/em&gt; to be both accessible and accurate - so often an author may have an accurate picture of a complex topic, but have to simplify it in their writing, particularly for an introductory book like Fluent C#. But there are limits - and in my view this book goes well past the level of error that I&amp;#39;m willing to put up with.&lt;/p&gt;  &lt;h3&gt;Conclusion&lt;/h3&gt;  &lt;p&gt;I really don&amp;#39;t &lt;em&gt;like&lt;/em&gt; ranting. I don&amp;#39;t &lt;em&gt;like&lt;/em&gt; sounding mean - and I &lt;em&gt;wanted&lt;/em&gt; to like this book. While I like &lt;a href="http://www.amazon.com/dp/0596800959"&gt;C# 4.0 in a Nutshell&lt;/a&gt; and &lt;a href="http://www.amazon.com/dp/0321694694"&gt;Essential C# 4.0&lt;/a&gt;, I&amp;#39;m still looking for a book which I can recommend to readers who want a more &amp;quot;lively&amp;quot; kind of book. Unfortunately I really can&amp;#39;t recommend Fluent C# to anyone - it is simply too inaccurate, and I believe it will cause confusion and instil bad habits in its readers.&lt;/p&gt;  &lt;p&gt;So, what next? I&amp;#39;m &lt;em&gt;hoping&lt;/em&gt; that the publisher and author will take my errata on board for the next printing, and revise it thoroughly. At that point I still don&amp;#39;t think I&amp;#39;d actually &lt;em&gt;like&lt;/em&gt; the book due to its structure and WPF focus (and the colour scheme, which I don&amp;#39;t expect to change), but it would at least be more a matter of taste then.&lt;/p&gt;  &lt;p&gt;I have some reason to be hopeful - because my review of Head-First C# was somewhat like this one, and one of the authors of that book (Andrew Stellman) was &lt;em&gt;incredibly&lt;/em&gt; good about the whole thing, and as a result the &lt;a href="http://www.amazon.com/dp/1449380344"&gt;second edition of Head-First C#&lt;/a&gt; is a &lt;em&gt;much&lt;/em&gt; better book than the first edition. Again, it&amp;#39;s not quite my preferred style, but for readers who like that sort of thing, it&amp;#39;s a much better option than Fluent C# at the moment, and one I&amp;#39;m happy to recommend (with the express caveat of getting the second edition).&lt;/p&gt;  &lt;p&gt;At the same time, reading Fluent C# (and particularly thinking about its debugger-first approach) has set me something of a challenge. You see, I&amp;#39;ve mostly avoided writing for new programmers so far - but I feel it&amp;#39;s &lt;em&gt;really&lt;/em&gt; important to get folks off on the right foot, and I&amp;#39;d like to have a stab at it. In particular, I would like to see if it&amp;#39;s possible to write an introductory text which teaches C# using unit tests wherever possible... but without being dry. Can we have a &amp;quot;fun&amp;quot; but accurate book, which tries to teach C# from scratch without giving the impression that user interfaces are the be-all and end-all of programming? Can I write in a way which is more personal but doesn&amp;#39;t feel artificial? I can&amp;#39;t see myself starting such a project any time in the next year, but maybe some time in 2013... Watch this space. In the meantime, I&amp;#39;ll keep an eye out for any more introductory books which might be more promising than Fluent C#.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1803256" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/wg2DJGKWqNY" height="1" width="1"/&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Book+reviews/default.aspx">Book reviews</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2011/12/05/book-review-fluent-c-rebecca-riordan-sams.aspx</feedburner:origLink></item><item><title>Eduasync part 17: unit testing</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/DnRbqgbMyKE/eduasync-part-17-unit-testing.aspx</link><pubDate>Fri, 25 Nov 2011 21:46:45 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1802928</guid><dc:creator>skeet</dc:creator><slash:comments>6</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1802928</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1802928</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2011/11/25/eduasync-part-17-unit-testing.aspx#comments</comments><description>&lt;p&gt;In the &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2011/11/22/eduasync-part-16-example-of-composition-majority-voting.aspx"&gt;last post&lt;/a&gt; I showed a method to implement &amp;quot;majority voting&amp;quot; for tasks, allowing a result to become available as soon as possible. At the end, I mentioned that I was reasonably confident that it worked because of the unit tests... but I didn&amp;#39;t show the tests themselves. I felt they deserved their own post, as there&amp;#39;s a bigger point here: &lt;em&gt;it&amp;#39;s possible to unit test async code&lt;/em&gt;. At least sometimes.&lt;/p&gt;  &lt;p&gt;Testing code involving asynchrony is generally a pain. Introducing the exact order of events that you want is awkward, as is managing the threading within tests. With a few benefits with async methods:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;We know that the async method itself will only execute in a single thread at a time &lt;/li&gt;    &lt;li&gt;We can control the thread in which the async method will execute, &lt;em&gt;if&lt;/em&gt; it doesn&amp;#39;t configure its awaits explicitly &lt;/li&gt;    &lt;li&gt;Assuming the async method returns Task or Task&amp;lt;T&amp;gt;, we can check whether or not it&amp;#39;s finished &lt;/li&gt;    &lt;li&gt;Between Task&amp;lt;T&amp;gt; and TaskCompletionSource&amp;lt;T&amp;gt;, we have a way of injecting tasks that we understand &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Now in our sample method we have the benefit of passing in the tasks that will be awaited - but assuming you&amp;#39;re using some reasonably testable API to fetch any awaitables within your async method, you should be okay. (Admittedly in the current .NET framework that excludes rather a lot of classes... but the synchronous versions of those calls are also generally hard to test too.)&lt;/p&gt;  &lt;h3&gt;The plan&lt;/h3&gt;  &lt;p&gt;For our majority tests, we want to be able to see what happens in various scenarios, with tasks completing at different times and in different ways. Looking at the &lt;a href="http://code.google.com/p/eduasync/source/browse/src/MajorityVoting.Tests/WhenMajorityTest.cs"&gt;test cases I&amp;#39;ve implemented&lt;/a&gt; I have the following tests:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;NullSequenceOfTasks &lt;/li&gt;    &lt;li&gt;EmptySequenceOfTasks &lt;/li&gt;    &lt;li&gt;NullReferencesWithinSequence &lt;/li&gt;    &lt;li&gt;SimpleSuccess &lt;/li&gt;    &lt;li&gt;InputOrderIsIrrelevant &lt;/li&gt;    &lt;li&gt;MajorityWithSomeDisagreement &lt;/li&gt;    &lt;li&gt;MajorityWithFailureTask &lt;/li&gt;    &lt;li&gt;EarlyFailure &lt;/li&gt;    &lt;li&gt;NoMajority &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I&amp;#39;m not going to claim this is a &lt;em&gt;comprehensive&lt;/em&gt; set of possible tests - it&amp;#39;s a proof of concept more than anything else. Let&amp;#39;s take one test as an example: MajorityWithFailureTask. The aim of this is to pass three tasks (of type Task&amp;lt;string&amp;gt;) into the method. One will give a result of &amp;quot;x&amp;quot;, the second will fail with an exception, and the third will also give a result of &amp;quot;x&amp;quot;. The events will occur in that order, and only when all three results are in should the returned task complete, at which point it will also have a success result of &amp;quot;x&amp;quot;.&lt;/p&gt;  &lt;p&gt;So, the tricky bit (compared with normal testing) is introducing the timing. We want to make it &lt;em&gt;appear&lt;/em&gt; as if tasks are completing in a particular order, at predetermined times, so we can check the state of the result between events.&lt;/p&gt;  &lt;h3&gt;Introducing the TimeMachine class&lt;/h3&gt;  &lt;p&gt;Okay, so it&amp;#39;s a silly name. But the basic idea is to have something to control the logical flow of time through our test. We&amp;#39;re going to ask the TimeMachine to &lt;em&gt;provide&lt;/em&gt; us with tasks which will act in a particular way at a given time, and then when we&amp;#39;ve started our async method we can then ask it to move time forward, letting the tasks complete as they go. It&amp;#39;s probably best to look at the code for MajorityWithFailureTask first, and then see what the implementation of TimeMachine looks like. Here&amp;#39;s the test:&lt;/p&gt;  &lt;div class="code"&gt;[Test]    &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; MajorityWithFailureTask()     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Linq"&gt;var&lt;/span&gt; timeMachine = &lt;span class="Keyword"&gt;new&lt;/span&gt; TimeMachine();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Second task gives a different result&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Linq"&gt;var&lt;/span&gt; task1 = timeMachine.AddSuccessTask(1, &lt;span class="String"&gt;&amp;quot;x&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Linq"&gt;var&lt;/span&gt; task2 = timeMachine.AddFaultingTask&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt;(2, &lt;span class="Keyword"&gt;new&lt;/span&gt; Exception(&lt;span class="String"&gt;&amp;quot;Bang!&amp;quot;&lt;/span&gt;));     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Linq"&gt;var&lt;/span&gt; task3 = timeMachine.AddSuccessTask(3, &lt;span class="String"&gt;&amp;quot;x&amp;quot;&lt;/span&gt;);     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Linq"&gt;var&lt;/span&gt; resultTask = MoreTaskEx.WhenMajority(task1, task2, task3);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Assert.IsFalse(resultTask.IsCompleted);     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Only one result so far - no consensus&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; timeMachine.AdvanceTo(1);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Assert.IsFalse(resultTask.IsCompleted);     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Second result is a failure&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; timeMachine.AdvanceTo(2);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Assert.IsFalse(resultTask.IsCompleted);     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Third result gives majority verdict&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; timeMachine.AdvanceTo(3);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Assert.AreEqual(TaskStatus.RanToCompletion, resultTask.Status);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Assert.AreEqual(&lt;span class="String"&gt;&amp;quot;x&amp;quot;&lt;/span&gt;, resultTask.Result);     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;As you can see, there are two types of method:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;AddSuccessTask / AddFaultingTask / AddCancelTask (not used here) - these all take the time at which they&amp;#39;re going to complete as their first parameter, and the method name describes the state they&amp;#39;ll reach on completion. The methods &lt;em&gt;return&lt;/em&gt; the task created by the time machine, ready to pass into the production code we&amp;#39;re testing. &lt;/li&gt;    &lt;li&gt;AdvanceTo / AdvanceBy (not used here) - make the time machine &amp;quot;advance time&amp;quot;, completing pre-programmed tasks as it goes. When those tasks complete, any continuations attached to them also execute, which is how the whole thing hangs together. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Now forcing tasks to complete is actually pretty simple, if you build them out of TaskCompletionSource&amp;lt;T&amp;gt; to start with. So all we need to do is keep our tasks in &amp;quot;time&amp;quot; order (which I achieve with SortedList), and then when we&amp;#39;re asked to advance time we move through the list and take the appropriate action for all the tasks which &lt;em&gt;weren&amp;#39;t&lt;/em&gt; completed before, but are now. I represent the &amp;quot;appropriate action&amp;quot; as a simple Action, which is built with a lambda expression from each of the Add methods. It&amp;#39;s really simple:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; TimeMachine    &lt;br /&gt;{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;int&lt;/span&gt; currentTime = 0;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; SortedList&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;, Action&amp;gt; actions = &lt;span class="Keyword"&gt;new&lt;/span&gt; SortedList&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;, Action&amp;gt;();    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;int&lt;/span&gt; CurrentTime { get { &lt;span class="Statement"&gt;return&lt;/span&gt; currentTime; } }    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; AdvanceBy(&lt;span class="ValueType"&gt;int&lt;/span&gt; time)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AdvanceTo(currentTime + time);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; AdvanceTo(&lt;span class="ValueType"&gt;int&lt;/span&gt; time)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Okay, not terribly efficient, but it&amp;#39;s simple.&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;foreach&lt;/span&gt; (&lt;span class="Linq"&gt;var&lt;/span&gt; entry &lt;span class="Statement"&gt;in&lt;/span&gt; actions)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;if&lt;/span&gt; (entry.Key &amp;gt; currentTime &amp;amp;&amp;amp; entry.Key &amp;lt;= time)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; entry.Value();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; currentTime = time;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt; Task&amp;lt;T&amp;gt; AddSuccessTask&amp;lt;T&amp;gt;(&lt;span class="ValueType"&gt;int&lt;/span&gt; time, T result)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; TaskCompletionSource&amp;lt;T&amp;gt; tcs = &lt;span class="Keyword"&gt;new&lt;/span&gt; TaskCompletionSource&amp;lt;T&amp;gt;();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; actions[time] = () =&amp;gt; tcs.SetResult(result);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; tcs.Task;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt; Task&amp;lt;T&amp;gt; AddCancelTask&amp;lt;T&amp;gt;(&lt;span class="ValueType"&gt;int&lt;/span&gt; time)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; TaskCompletionSource&amp;lt;T&amp;gt; tcs = &lt;span class="Keyword"&gt;new&lt;/span&gt; TaskCompletionSource&amp;lt;T&amp;gt;();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; actions[time] = () =&amp;gt; tcs.SetCanceled();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; tcs.Task;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt; Task&amp;lt;T&amp;gt; AddFaultingTask&amp;lt;T&amp;gt;(&lt;span class="ValueType"&gt;int&lt;/span&gt; time, Exception e)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; TaskCompletionSource&amp;lt;T&amp;gt; tcs = &lt;span class="Keyword"&gt;new&lt;/span&gt; TaskCompletionSource&amp;lt;T&amp;gt;();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; actions[time] = () =&amp;gt; tcs.SetException(e);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; tcs.Task;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;Okay, that&amp;#39;s a fair amount of code for a blog posts (and yes, it could do with some doc comments etc!) but considering that it makes life testable, it&amp;#39;s pretty simple.&lt;/p&gt;  &lt;p&gt;So, is that it?&lt;/p&gt;  &lt;h3&gt;It works on my machine... with my test runner... in simple cases...&lt;/h3&gt;  &lt;p&gt;When I first ran the tests using TimeMachine, they worked almost immediately. This didn&amp;#39;t surprise me nearly as much as it should have done. You see, when the tests execute, they use async/await in the normal way - which means the continuations are scheduled on &amp;quot;the current task scheduler&amp;quot;. &lt;em&gt;I have no idea what the current task scheduler is in unit tests&lt;/em&gt;. Or rather, it feels like something which is implementation specific. It could easily have worked when running the tests from ReSharper, but not from NCrunch, or not from the command line NUnit test runner.&lt;/p&gt;  &lt;p&gt;As it happens, I believe all of these run tests on thread pool threads with no task scheduler allocated, which means that the continuation is attached to the task to complete &amp;quot;in-line&amp;quot; - so when the TimeMachine sets the result on a TaskCompletionSource, the continuations execute before that call returns. That means everything happens on one thread, with no ambiguity or flakiness - yay!&lt;/p&gt;  &lt;p&gt;However, there are two problems:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The words &amp;quot;I believe&amp;quot; aren&amp;#39;t exactly confidence-inspiring when it comes to testing that your software works correctly.&lt;/li&gt;    &lt;li&gt;Our majority voting code only ever sees one completed task at a time - we&amp;#39;re not testing the situation where several tasks complete so quickly together that the continuation doesn&amp;#39;t get chance to run before they&amp;#39;ve all finished.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Both of these are solvable with a custom TaskScheduler or SynchronizationContext. Without diving into the docs, I&amp;#39;m not sure yet which I&amp;#39;ll need, but the aim will be:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Make TimeMachine implement IDisposable&lt;/li&gt;    &lt;li&gt;In the constructor, set the current SynchronizationContext (or TaskScheduler) to a custom one having remembered what the previous one was&lt;/li&gt;    &lt;li&gt;On disposal, reset the context&lt;/li&gt;    &lt;li&gt;Make the custom scheduler keep a queue of jobs, such that when we&amp;#39;re asked to advance to time T, we complete &lt;em&gt;all&lt;/em&gt; the appropriate tasks but don&amp;#39;t execute any continuations, then we execute all the pending continuations.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I don&amp;#39;t yet know how hard it will be, but hopefully the &lt;a href="http://code.msdn.microsoft.com/ParExtSamples"&gt;Parallel Extensions Samples&lt;/a&gt; will help me.&lt;/p&gt;  &lt;h3&gt;Conclusion&lt;/h3&gt;  &lt;p&gt;I&amp;#39;m not going to claim this is &amp;quot;the&amp;quot; way of unit testing asynchronous methods. It&amp;#39;s clearly a proof-of-concept implementation of what can only be called a &amp;quot;test framework&amp;quot; in the loosest possible sense. However, I hope it gives &lt;em&gt;an&lt;/em&gt; example of a path we might take. I&amp;#39;m looking forward to seeing what others come up with, along with rather more polished implementations.&lt;/p&gt;  &lt;p&gt;Next time, I&amp;#39;m going to shamelessly steal an idea that a reader mailed me (with permission, of course). It&amp;#39;s insanely cool, simple and yet slightly brain-bending, and I suspect will handy in many situations. Love it.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1802928" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/DnRbqgbMyKE" height="1" width="1"/&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+5/default.aspx">C# 5</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/async/default.aspx">async</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Eduasync/default.aspx">Eduasync</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2011/11/25/eduasync-part-17-unit-testing.aspx</feedburner:origLink></item><item><title>Eduasync part 16: Example of composition: majority voting</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/6Sacmt2nzms/eduasync-part-16-example-of-composition-majority-voting.aspx</link><pubDate>Tue, 22 Nov 2011 23:18:41 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1802817</guid><dc:creator>skeet</dc:creator><slash:comments>4</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1802817</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1802817</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2011/11/22/eduasync-part-16-example-of-composition-majority-voting.aspx#comments</comments><description>&lt;p&gt;Note: For the rest of this series, I&amp;#39;ll be veering away from the original purpose of the project (investigating what the compiler is up to) in favour of discussing the feature itself. As such, I&amp;#39;ve added a requirement for AsyncCtpLib.dll - but due to potential distribution restrictions, I&amp;#39;ve felt it safest &lt;em&gt;not&lt;/em&gt; to include that in the source repository. If you&amp;#39;re running this code yourself, you&amp;#39;ll need to copy the DLL from your installation location into the Eduasync\lib directory before it will build - or change each reference to it.&lt;/p&gt;  &lt;p&gt;One of the things I &lt;em&gt;love&lt;/em&gt; about async is the compositional aspect. This is partly due to the way that the Task Parallel Library encourages composition to start with, but async/await makes it even easier by building the tasks for you. In the next few posts I&amp;#39;ll talk about a few examples of interesting building blocks. I wouldn&amp;#39;t be surprised to see an open source library with a &lt;em&gt;proper&lt;/em&gt; implementation of some of these ideas (Eduasync is &lt;em&gt;not&lt;/em&gt; designed for production usage) whether from Microsoft or a third party.&lt;/p&gt;  &lt;p&gt;In &lt;a href="http://code.google.com/p/eduasync/source/browse/#hg%2Fsrc%2FMajorityVoting"&gt;project 26 of Eduasync&lt;/a&gt;, I&amp;#39;ve implemented &amp;quot;majority voting&amp;quot; via composition. The basic idea is simple, and the motivation should be reasonably obvious in this day and age of redundant services. You have (say) five different tasks which are meant to be computing the same thing. As soon as you have a single answer which the majority of the tasks agree on, the code which needs the result can continue. If the tasks disagree, or fail (or a combination leading to no single successful majority result), the overall result is failure too.&lt;/p&gt;  &lt;p&gt;My personal experience with services requiring a majority of operations to return is with &lt;a href="http://static.googleusercontent.com/external_content/untrusted_dlcp/research.google.com/en//pubs/archive/36971.pdf"&gt;Megastore&lt;/a&gt;, a storage system we use at Google. I&amp;#39;m not going to pretend to understand half of the details of how Megastore works, and I&amp;#39;m certainly not about to reveal any confidential information about its internals or indeed how we use it, but basically when discussing it with colleagues at around the time that async was announced, I contemplated what a handy feature async would be when implementing a Megastore client. It could also be used in systems where each calculation is performed in triplicate to guard against rogue errors - although I suspect the chances of those systems being implemented in C# are pretty small.&lt;/p&gt;  &lt;p&gt;It&amp;#39;s worth mentioning that the implementation here &lt;em&gt;wouldn&amp;#39;t&lt;/em&gt; be appropriate for something like a stock price service, where the result can change rapidly and you may be happy to tolerate a &lt;em&gt;small&lt;/em&gt; discrepancy, within some bounds.&lt;/p&gt;  &lt;h3&gt;The API&lt;/h3&gt;  &lt;p&gt;Here&amp;#39;s the signatures of the methods we&amp;#39;ll implement:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt; Task&amp;lt;T&amp;gt; WhenMajority&amp;lt;T&amp;gt;(&lt;span class="MethodParameter"&gt;params&lt;/span&gt; Task&amp;lt;T&amp;gt;[] tasks)     &lt;br /&gt;    &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt; Task&amp;lt;T&amp;gt; WhenMajority&amp;lt;T&amp;gt;(IEnumerable&amp;lt;Task&amp;lt;T&amp;gt;&amp;gt; tasks) &lt;/div&gt;  &lt;p&gt;Obviously the first just delegates to the second, but it&amp;#39;s helpful to have both forms, so that we can pass in a few tasks in an ad hoc manner with the first overload, or a LINQ-generated sequence of tasks with the second.&lt;/p&gt;  &lt;p&gt;The name is a little odd - it&amp;#39;s meant to match WhenAll and WhenAny, but I&amp;#39;m sure there are better options. I&amp;#39;m not terribly interested in that at the moment.&lt;/p&gt;  &lt;p&gt;It&amp;#39;s easy to use within an async method:&lt;/p&gt;  &lt;div class="code"&gt;Task&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt; firstTask = firstServer.ComputeSomethingAsync(input);     &lt;br /&gt;Task&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt; secondTask = selectServer.ComputeSomethingAsync(input);     &lt;br /&gt;Task&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt; thirdTask = thirdServer.ComputeSomethingAsync(input);     &lt;br /&gt;    &lt;br /&gt;&lt;span class="ValueType"&gt;int&lt;/span&gt; result = &lt;span class="Modifier"&gt;await&lt;/span&gt; MoreTaskEx.WhenMajority(firstTask, secondTask, thirdTask); &lt;/div&gt;  &lt;p&gt;Or using the LINQ-oriented overload:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Linq"&gt;var&lt;/span&gt; tasks = servers.Select(server =&amp;gt; server.ComputeSomethingAsync(input));     &lt;br /&gt;&lt;span class="ValueType"&gt;int&lt;/span&gt; result = &lt;span class="Modifier"&gt;await&lt;/span&gt; MoreTaskEx.WhenMajority(tasks); &lt;/div&gt;  &lt;p&gt;Of course we could add an extension method (dropping the When prefix as it doesn&amp;#39;t make as much sense there, IMO):&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="ValueType"&gt;int&lt;/span&gt; result = &lt;span class="Modifier"&gt;await&lt;/span&gt; servers.Select(server =&amp;gt; server.ComputeSomethingAsync(input))     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .MajorityAsync(); &lt;/div&gt;  &lt;p&gt;The fact that we&amp;#39;ve stayed within the Task&amp;lt;T&amp;gt; model is what makes it all work so smoothly. We couldn&amp;#39;t easily express the same API for other awaitable types &lt;em&gt;in general&lt;/em&gt; although we could do it for any other &lt;em&gt;specific&lt;/em&gt; awaitable type of course. It&amp;#39;s possible that it would work using dynamic, but I&amp;#39;d rather avoid that :) Let&amp;#39;s implement it now.&lt;/p&gt;  &lt;h3&gt;Implementation&lt;/h3&gt;  &lt;p&gt;There are two parts to the implementation, in the same way that we implemented LINQ operators in &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/tags/Edulinq/default.aspx"&gt;Edulinq&lt;/a&gt; - and for the same reason. We want to go bang &lt;em&gt;immediately&lt;/em&gt; if there are any clear input violations - such as the sequence of tasks being null or empty. This is in line with the &lt;a href="http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;amp;id=19957"&gt;Task-based Asynchronous Pattern white paper&lt;/a&gt;:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;An asynchronous method should only directly raise an exception to be thrown out of the &lt;i&gt;MethodName&lt;/i&gt;Async call in response to a &lt;i&gt;usage&lt;/i&gt; error*. For all other errors, exceptions occurring during the execution of an asynchronous method should be assigned to the returned Task. &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Now it occurs to me that we don&amp;#39;t &lt;em&gt;really&lt;/em&gt; need to do this in two separate methods (one for precondition checking, one for real work). We &lt;em&gt;could&lt;/em&gt; create an async lambda expression of type Func&amp;lt;Task&amp;lt;T&amp;gt;&amp;gt;, and make the method just return the result of invoking it - but I don&amp;#39;t think that would be great in terms of readability.&lt;/p&gt;  &lt;p&gt;So, the first part of the implementation performing validation is really simple:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt; Task&amp;lt;T&amp;gt; WhenMajority&amp;lt;T&amp;gt;(&lt;span class="MethodParameter"&gt;params&lt;/span&gt; Task&amp;lt;T&amp;gt;[] tasks)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; WhenMajority((IEnumerable&amp;lt;Task&amp;lt;T&amp;gt;&amp;gt;) tasks);     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt; Task&amp;lt;T&amp;gt; WhenMajority&amp;lt;T&amp;gt;(IEnumerable&amp;lt;Task&amp;lt;T&amp;gt;&amp;gt; tasks)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;if&lt;/span&gt; (tasks == &lt;span class="Keyword"&gt;null&lt;/span&gt;)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;throw&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; ArgumentNullException(&lt;span class="String"&gt;&amp;quot;tasks&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; List&amp;lt;Task&amp;lt;T&amp;gt;&amp;gt; taskList = &lt;span class="Keyword"&gt;new&lt;/span&gt; List&amp;lt;Task&amp;lt;T&amp;gt;&amp;gt;(tasks);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;if&lt;/span&gt; (taskList.Count == 0)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;throw&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; ArgumentException(&lt;span class="String"&gt;&amp;quot;Empty sequence of tasks&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;foreach&lt;/span&gt; (&lt;span class="Linq"&gt;var&lt;/span&gt; task &lt;span class="Statement"&gt;in&lt;/span&gt; taskList)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;if&lt;/span&gt; (task == &lt;span class="Keyword"&gt;null&lt;/span&gt;)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;throw&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; ArgumentException(&lt;span class="String"&gt;&amp;quot;Null task in sequence&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; WhenMajorityImpl(taskList);     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;The interesting part is obviously in WhenMajorityImpl. It&amp;#39;s &lt;em&gt;mildly&lt;/em&gt; interesting to note that I create a copy of the sequence passed in to start with - I know I&amp;#39;ll need it in a fairly concrete form, so it&amp;#39;s appropriate to remove any laziness at this point.&lt;/p&gt;  &lt;p&gt;So, here&amp;#39;s WhenMajorityImpl, which I&amp;#39;ll then explain:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;async&lt;/span&gt; Task&amp;lt;T&amp;gt; WhenMajorityImpl&amp;lt;T&amp;gt;(List&amp;lt;Task&amp;lt;T&amp;gt;&amp;gt; tasks)    &lt;br /&gt;{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Need a real majority - so for 4 or 5 tasks, must have 3 equal results.&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ValueType"&gt;int&lt;/span&gt; majority = (tasks.Count / 2) + 1;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ValueType"&gt;int&lt;/span&gt; failures = 0;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ValueType"&gt;int&lt;/span&gt; bestCount = 0;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Dictionary&amp;lt;T, &lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt; results = &lt;span class="Keyword"&gt;new&lt;/span&gt; Dictionary&amp;lt;T, &lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt;();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; List&amp;lt;Exception&amp;gt; exceptions = &lt;span class="Keyword"&gt;new&lt;/span&gt; List&amp;lt;Exception&amp;gt;();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;while&lt;/span&gt; (&lt;span class="Keyword"&gt;true&lt;/span&gt;)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;await&lt;/span&gt; TaskEx.WhenAny(tasks);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Linq"&gt;var&lt;/span&gt; newTasks = &lt;span class="Keyword"&gt;new&lt;/span&gt; List&amp;lt;Task&amp;lt;T&amp;gt;&amp;gt;();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;foreach&lt;/span&gt; (&lt;span class="Linq"&gt;var&lt;/span&gt; task &lt;span class="Statement"&gt;in&lt;/span&gt; tasks)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;switch&lt;/span&gt; (task.Status)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;case&lt;/span&gt; TaskStatus.Canceled:    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; failures++;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;break&lt;/span&gt;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;case&lt;/span&gt; TaskStatus.Faulted:    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; failures++;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; exceptions.Add(task.Exception.Flatten());    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;break&lt;/span&gt;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;case&lt;/span&gt; TaskStatus.RanToCompletion:    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ValueType"&gt;int&lt;/span&gt; count;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Doesn&amp;#39;t matter whether it was there before or not - we want 0 if not anyway&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; results.TryGetValue(task.Result, &lt;span class="MethodParameter"&gt;out&lt;/span&gt; count);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; count++;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;if&lt;/span&gt; (count &amp;gt; bestCount)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; bestCount = count;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;if&lt;/span&gt; (count &amp;gt;= majority)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; task.Result;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; results[task.Result] = count;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;break&lt;/span&gt;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;default&lt;/span&gt;:    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Keep going next time. may not be appropriate for Created&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; newTasks.Add(task);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;break&lt;/span&gt;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// The new list of tasks to wait for&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; tasks = newTasks;    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// If we can&amp;#39;t possibly work, bail out.&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;if&lt;/span&gt; (tasks.Count + bestCount &amp;lt; majority)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;throw&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; AggregateException(&lt;span class="String"&gt;&amp;quot;No majority result possible&amp;quot;&lt;/span&gt;, exceptions);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;I should warn you that this &lt;em&gt;isn&amp;#39;t&lt;/em&gt; a particularly efficient implementation - it was just one I wrote until it worked. The basic steps are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Work out how many results make a majority, so we know when to stop&lt;/li&gt;    &lt;li&gt;Keep track of how many &amp;quot;votes&amp;quot; our most commonly-returned result has, along with the counts of &lt;em&gt;all&lt;/em&gt; the votes&lt;/li&gt;    &lt;li&gt;Repeatedly:&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;Wait (asynchronously) for &lt;em&gt;at least&lt;/em&gt; of the remaining tasks to finish (many may finish &amp;quot;at the same time&amp;quot;)&lt;/li&gt;      &lt;li&gt;Start a new list of &amp;quot;tasks we&amp;#39;re going to wait for next time&amp;quot;&lt;/li&gt;      &lt;li&gt;Process each task in the current list, taking an action on each state:&lt;/li&gt;      &lt;ul&gt;       &lt;li&gt;If it&amp;#39;s been cancelled, we&amp;#39;ll treat that as a failure (we could potentially treat &amp;quot;the majority have been cancelled&amp;quot; as a cancellation, but for the moment a failure is good enough)&lt;/li&gt;        &lt;li&gt;If it&amp;#39;s faulted, we&amp;#39;ll add the exception to the list of exceptions, so that if the overall result ends up as failure, we can throw an AggregateException with all of the individual exceptions&lt;/li&gt;        &lt;li&gt;If it&amp;#39;s finished successfully, we&amp;#39;ll check the result:&lt;/li&gt;        &lt;ul&gt;         &lt;li&gt;Add 1 to the count for that result (the dictionary will use the default comparer for the result type, which we assume is good enough)&lt;/li&gt;          &lt;li&gt;If this is greater than the previous &amp;quot;winner&amp;quot; (which could be for the same result), check for it being actually an overall majority, and return if so.&lt;/li&gt;       &lt;/ul&gt;        &lt;li&gt;If it&amp;#39;s still running (or starting), add it to the new task list&lt;/li&gt;     &lt;/ul&gt;      &lt;li&gt;Check whether enough tasks have failed - or given different results - so ensure that a majority is now impossible. If so, throw an AggregateException to say so. This &lt;em&gt;may&lt;/em&gt; have some exceptions, but it may not (if there are three tasks which gave different results, none of them actually &lt;em&gt;failed&lt;/em&gt;)&lt;/li&gt;   &lt;/ul&gt; &lt;/ul&gt;  &lt;p&gt;Each iteration of the &amp;quot;repeatedly&amp;quot; will have a smaller list to check than before, so we&amp;#39;ll definitely terminate at some point.&lt;/p&gt;  &lt;p&gt;I mentioned that it&amp;#39;s inefficient. In particular, we&amp;#39;re ignoring the fact that WhenAny returns a Task&amp;lt;Task&amp;lt;T&amp;gt;&amp;gt;, so awaiting that will actually tell us &lt;em&gt;a&lt;/em&gt; task which has finished. We don&amp;#39;t need to loop over the whole collection at that point - we could just remove that single task from the collection. We could do that efficiently if we kept a Dictionary&amp;lt;Task&amp;lt;T&amp;gt;, LinkedListNode&amp;lt;Task&amp;lt;T&amp;gt;&amp;gt; and a LinkedList&amp;lt;Task&amp;lt;T&amp;gt;&amp;gt; - we&amp;#39;d just look up the task which had completed in the dictionary, remove its node from the list, and remove the entry from the dictionary. We wouldn&amp;#39;t need to create a new collection each time, or iterate through all of the old one. However, that&amp;#39;s a job for another day... as is allowing a cancellation token to be passed in, and a custom equality comparer.&lt;/p&gt;  &lt;h3&gt;Conclusion&lt;/h3&gt;  &lt;p&gt;So we can make this implementation smarter and more flexible, certainly - but it&amp;#39;s not insanely tricky to write. I&amp;#39;m &lt;em&gt;reasonably&lt;/em&gt; confident that it works, too - as I have unit tests for it. They&amp;#39;ll come in the next part. The important point&amp;#160; from this post is that by sticking within the Task&amp;lt;T&amp;gt; world, we can &lt;em&gt;reasonably easily&lt;/em&gt; create building blocks to allow for composition of asynchronous operations. While it would be nice to have someone more competent than myself write a bullet-proof, efficient implementation of this operation, I wouldn&amp;#39;t feel too unhappy using a homegrown one in production. The same could &lt;em&gt;not&lt;/em&gt; have been said pre-async/await. I just wouldn&amp;#39;t have had a chance of getting it right.&lt;/p&gt;  &lt;p&gt;Next up - the unit tests for this code, in which I introduce the TimeMachine class.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1802817" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/6Sacmt2nzms" height="1" width="1"/&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/async/default.aspx">async</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Eduasync/default.aspx">Eduasync</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2011/11/22/eduasync-part-16-example-of-composition-majority-voting.aspx</feedburner:origLink></item><item><title>Eduasync part 15: implementing COMEFROM with a horrible hack</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/RPulVO5m5sM/eduasync-part-15-implementing-comefrom-with-a-horrible-hack.aspx</link><pubDate>Fri, 11 Nov 2011 22:15:11 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1802363</guid><dc:creator>skeet</dc:creator><slash:comments>6</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1802363</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1802363</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2011/11/11/eduasync-part-15-implementing-comefrom-with-a-horrible-hack.aspx#comments</comments><description>&lt;p&gt;Ages ago when I wrote my &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2011/06/29/eduasync-part-14-data-passing-in-coroutines.aspx"&gt;previous Eduasync post&lt;/a&gt;, I said we&amp;#39;d look at a pipeline model of coroutines. I&amp;#39;ve decided to skip that, as I &lt;em&gt;do&lt;/em&gt; want to cover the topic of this post, and I&amp;#39;ve got some more &amp;quot;normal&amp;quot; async ideas to write about too. If you want to look at the pipeline coroutines code, it&amp;#39;s &lt;a href="http://code.google.com/p/eduasync/source/browse/#hg%2Fsrc%2FPipelineCoroutines"&gt;project 20&lt;/a&gt; in the source repository. Have fun, and don&amp;#39;t blame me if you get confused reading it - so do I.&lt;/p&gt;  &lt;p&gt;The code I &lt;em&gt;am&lt;/em&gt; going to write about is horrible too. It&amp;#39;s almost as tricky to understand, and it does &lt;em&gt;far&lt;/em&gt; nastier things. Things that the C# 5 specification explicitly says you shouldn&amp;#39;t do.&lt;/p&gt;  &lt;p&gt;If it makes you feel any better when your head hurts reading this code, spare a thought for me - I haven&amp;#39;t looked at it in over six months, and &lt;em&gt;I&lt;/em&gt; don&amp;#39;t have a blog post explaining how it&amp;#39;s meant to work. I just have almost entirely uncommented code which is &lt;em&gt;designed&lt;/em&gt; to be hard to understand (in terms of the main program flow).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;On no account should any code like this ever be used for anything remotely serious.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;With that health warning out of the way, let&amp;#39;s have a look at it...&lt;/p&gt;  &lt;h3&gt;COMEFROM at the caller level&lt;/h3&gt;  &lt;p&gt;The idea is to implement the &lt;a href="http://en.wikipedia.org/wiki/COMEFROM"&gt;COMEFROM&lt;/a&gt; control structure, which is sort of the opposite of GOTO (or in my implementation, more of a GOSUB). There are two operations, effectively:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;ComeFrom(label): Register interest in a particular label. &lt;/li&gt;    &lt;li&gt;Label(label): If anyone has registered interested in the given label, keep going from their registration point (which will be within a method), then continue from where we left off afterwards. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;In some senses it&amp;#39;s a little like the observer pattern, with labels taking the place of events. However, it looks entirely different and is &lt;em&gt;much&lt;/em&gt; harder to get your head round, because instead of having a nicely-encapsulated action which is subscribed to an event, we just have a ComeFrom call which lets us jump back into a method somewhat arbitrarily.&lt;/p&gt;  &lt;p&gt;I have two implementations, in &lt;a href="http://code.google.com/p/eduasync/source/browse/#hg%2Fsrc%2FComeFromCoroutines"&gt;project 22&lt;/a&gt; and &lt;a href="http://code.google.com/p/eduasync/source/browse/#hg%2Fsrc%2FStateSavingComeFrom"&gt;project 23&lt;/a&gt; in source control. Project 22 is almost sane; a little funky, but not too bad. Project 23 is where the fun really happens. In addition to the operations listed above, there&amp;#39;s an Execute operation which is sort of an implementation detail - it allows an async method containing ComeFrom calls to be executed without returning earlier than we might want.&lt;/p&gt;  &lt;p&gt;Let&amp;#39;s look at some code and the output, and try to work out what&amp;#39;s going on.&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;internal&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Program     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; Main(&lt;span class="ReferenceType"&gt;string&lt;/span&gt;[] args)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Coordinator coordinator = &lt;span class="Keyword"&gt;new&lt;/span&gt; Coordinator(SimpleEntryPoint);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; coordinator.Start();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;async&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; SimpleEntryPoint(Coordinator coordinator)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;await&lt;/span&gt; coordinator.Execute(SimpleOtherMethod);     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;First call to Label(x)&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;await&lt;/span&gt; coordinator.Label(&lt;span class="String"&gt;&amp;quot;x&amp;quot;&lt;/span&gt;);     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Second call to Label(x)&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;await&lt;/span&gt; coordinator.Label(&lt;span class="String"&gt;&amp;quot;x&amp;quot;&lt;/span&gt;);     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Registering interesting in y&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ValueType"&gt;bool&lt;/span&gt; firstTime = &lt;span class="Keyword"&gt;true&lt;/span&gt;;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;await&lt;/span&gt; coordinator.ComeFrom(&lt;span class="String"&gt;&amp;quot;y&amp;quot;&lt;/span&gt;);     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;After ComeFrom(y). FirstTime={0}&amp;quot;&lt;/span&gt;, firstTime);     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;if&lt;/span&gt; (firstTime)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; firstTime = &lt;span class="Keyword"&gt;false&lt;/span&gt;;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;await&lt;/span&gt; coordinator.Label(&lt;span class="String"&gt;&amp;quot;y&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Finished&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;async&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; SimpleOtherMethod(Coordinator coordinator)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Start of SimpleOtherMethod&amp;quot;&lt;/span&gt;);     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ValueType"&gt;int&lt;/span&gt; count = 0;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;await&lt;/span&gt; coordinator.ComeFrom(&lt;span class="String"&gt;&amp;quot;x&amp;quot;&lt;/span&gt;);     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;After ComeFrom x in SimpleOtherMethod. count={0}. Returning.&amp;quot;&lt;/span&gt;,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; count);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; count++;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;}&lt;/div&gt;  &lt;p&gt;The reason for the &amp;quot;Simple&amp;quot; prefix on the method names is that there&amp;#39;s another example in the same file, with a more complex control flow.&lt;/p&gt;  &lt;p&gt;Here&amp;#39;s the output - then we can look at why we&amp;#39;re getting it...&lt;/p&gt;  &lt;div class="output"&gt;Start of SimpleOtherMethod    &lt;br /&gt;After ComeFrom x in SimpleOtherMethod. count=0. Returning.     &lt;br /&gt;First call to Label(x)     &lt;br /&gt;After ComeFrom x in SimpleOtherMethod. count=1. Returning.     &lt;br /&gt;Second call to Label(x)     &lt;br /&gt;After ComeFrom x in SimpleOtherMethod. count=2. Returning.     &lt;br /&gt;Registering interesting in y     &lt;br /&gt;After ComeFrom(y). FirstTime=True     &lt;br /&gt;After ComeFrom(y). FirstTime=False     &lt;br /&gt;Finished &lt;/div&gt;  &lt;p&gt;So, the control flow is a bit like this:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Start SimpleEntryPoint      &lt;ul&gt;       &lt;li&gt;Call into SimpleOtherMethod          &lt;ul&gt;           &lt;li&gt;Log &amp;quot;Start of SimpleOtherMethod&amp;quot; &lt;/li&gt;            &lt;li&gt;Initialize the &amp;quot;count&amp;quot; variable with value 0 &lt;/li&gt;            &lt;li&gt;Register interest in x; ComeFrom remembers the continuation &lt;em&gt;but keeps going&lt;/em&gt;. &lt;/li&gt;            &lt;li&gt;Log &amp;quot;After ComeFrom x in SimpleOtherMethod. count=0. Returning.&amp;quot; &lt;/li&gt;            &lt;li&gt;Increment count to 1. &lt;/li&gt;            &lt;li&gt;Return. &lt;/li&gt;         &lt;/ul&gt;       &lt;/li&gt;        &lt;li&gt;Return takes us back to SimpleEntryPoint... &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Log &amp;quot;First call to Label(x)&amp;quot; &lt;/li&gt;    &lt;li&gt;Call Label(&amp;quot;x&amp;quot;)...      &lt;ul&gt;       &lt;li&gt;... which takes us back into SimpleOtherMethod (remember, the method we thought we&amp;#39;d finished executing?) just after ComeFrom          &lt;ul&gt;           &lt;li&gt;Log AfterComeFrom x in SimpleOtherMethod. count=1. Returning. &lt;/li&gt;            &lt;li&gt;Increment count to 2. &lt;/li&gt;            &lt;li&gt;Return. &lt;/li&gt;         &lt;/ul&gt;       &lt;/li&gt;        &lt;li&gt;Return takes us back to SimpleEntryPoint... &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Log &amp;quot;Second call to Label(x)&amp;quot; &lt;/li&gt;    &lt;li&gt;Call Label(&amp;quot;x&amp;quot;)...      &lt;ul&gt;       &lt;li&gt;... which takes us back into SimpleOtherMethod again          &lt;ul&gt;           &lt;li&gt;Log AfterComeFrom x in SimpleOtherMethod. count=2. Returning. &lt;/li&gt;            &lt;li&gt;Increment count to 3. &lt;/li&gt;            &lt;li&gt;Return. &lt;/li&gt;         &lt;/ul&gt;       &lt;/li&gt;        &lt;li&gt;Return takes us back to SimpleEntryPoint... &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Log &amp;quot;Registering interest in y&amp;quot; &lt;/li&gt;    &lt;li&gt;Initialize the &amp;quot;firstTime&amp;quot; variable with value true. &lt;/li&gt;    &lt;li&gt;Register interest in y; ComeFrom remembers the continuation and keeps going &lt;/li&gt;    &lt;li&gt;Log &amp;quot;After ComeFrom(y). FirstTime=True&amp;quot; &lt;/li&gt;    &lt;li&gt;Check the value of firstTime... It&amp;#39;s true, so:      &lt;ul&gt;       &lt;li&gt;Set firstTime to false &lt;/li&gt;        &lt;li&gt;Call Label(&amp;quot;y&amp;quot;) &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;... which takes us back to earlier in the method (just after ComeFrom), like a normal looping construct... &lt;/li&gt;    &lt;li&gt;Log &amp;quot;After ComeFrom(y). FirstTime=False&amp;quot; &lt;/li&gt;    &lt;li&gt;Check the value of firstTime... It&amp;#39;s false, so:      &lt;ul&gt;       &lt;li&gt;Log &amp;quot;Finished&amp;quot; &lt;/li&gt;        &lt;li&gt;Exit! &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Doing all of this has a few interesting challenges. Let&amp;#39;s look at them one at a time... and I would &lt;em&gt;strongly&lt;/em&gt; advise you not to try to pay too much attention to the details.&lt;/p&gt;  &lt;h3&gt;Noting a continuation and continuing regardless...&lt;/h3&gt;  &lt;p&gt;Just as a quick reminder before we get cracking, it&amp;#39;s worth remembering that &lt;em&gt;all&lt;/em&gt; of this is entirely synchronous, despite being implemented with async. There&amp;#39;s only a single user thread involved here. As with previous parts, we maintain a stack of actions to call, and basically keep calling from the top until we&amp;#39;re done - but the actions we call can create extra stack entries, of course.&lt;/p&gt;  &lt;p&gt;ComeFrom has unusual semantics in terms of async. We want to remember the continuation &lt;em&gt;and&lt;/em&gt; keep executing as if we didn&amp;#39;t need to wait. We can easily do one side or the other. If we wanted to just keep going without needing to know about the continuation, we could just return true from IsCompleted. If we just want to remember the continuation, we can make the awaiter&amp;#39;s IsCompleted property return false, and remember the continuation when it&amp;#39;s passed to OnCompleted. How do we do both?&lt;/p&gt;  &lt;p&gt;Well, effectively we want to remember the continuation and then call it immediately. But we can&amp;#39;t &lt;em&gt;just&lt;/em&gt; call it directly from OnCompleted, as otherwise each ComeFrom call would end up in a &amp;quot;real&amp;quot; execution stack from, whereas our execution stack is stored as a Stack&amp;lt;Action&amp;gt;. So instead, we need to remember the continuation and immediately put it at the top of the stack.&lt;/p&gt;  &lt;p&gt;However, that only works if as soon as the generated code returns from the async method containing the ComeFrom call, we go back into the state machine. If we&amp;#39;d just called SimpleOtherMethod directly in SimpleEntryPoint, we would have continued within SimpleEntryPoint with the new stack entry just waiting around. This is why we need the Executor method: that does exactly the same thing, effectively shuffling the stack around. When it&amp;#39;s given something to execute, it puts its own continuation on the action stack, &lt;em&gt;then&lt;/em&gt; the action it&amp;#39;s been asked to execute, then returns. The top level code will then pick up the original action, and we&amp;#39;re away.&lt;/p&gt;  &lt;p&gt;So, here&amp;#39;s the code for Execute, which is the simplest part of the coordinator:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt; ExecuteAwaiter Execute(Action&amp;lt;Coordinator&amp;gt; action)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; ExecuteAwaiter(() =&amp;gt; action(&lt;span class="Keyword"&gt;this&lt;/span&gt;), &lt;span class="Keyword"&gt;this&lt;/span&gt;);     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; ExecuteAwaiter     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; Action action;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; Coordinator coordinator;     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;internal&lt;/span&gt; ExecuteAwaiter(Action action, Coordinator coordinator)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Keyword"&gt;this&lt;/span&gt;.action = action;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Keyword"&gt;this&lt;/span&gt;.coordinator = coordinator;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt; ExecuteAwaiter GetAwaiter()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;this&lt;/span&gt;;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Always yield&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;bool&lt;/span&gt; IsCompleted { get { &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;false&lt;/span&gt;; } }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; OnCompleted(Action callerContinuation)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// We want to execute the action continuation, then get back here,&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// allowing any extra continuations put on the stack *within* the action&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// to be executed.&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; coordinator.stack.Push(callerContinuation);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; coordinator.stack.Push(action);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; GetResult()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;All the awaitables in this project return themselves as the awaiter - when you don&amp;#39;t need any other state, it&amp;#39;s an easy step to take.&lt;/p&gt;  &lt;p&gt;That&amp;#39;s all we need to say about Execute, but how exactly are we capturing the continuation in ComeFrom?&lt;/p&gt;  &lt;h3&gt;Capturing continuations&lt;/h3&gt;  &lt;p&gt;Once we&amp;#39;ve got the action stack shuffling under our belts, there are two more problems with ComeFrom:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;What happens if we ComeFrom the same label twice? &lt;/li&gt;    &lt;li&gt;How do we really capture a continuation? &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The first point didn&amp;#39;t come up in the sample I&amp;#39;ve shown here, but it &lt;em&gt;does&lt;/em&gt; come up in the more complex example - imagine if SimpleOtherMethod had two ComeFrom calls; when we jump back to the first one, we&amp;#39;ll execute the second one again. I made a simple policy decision to only allow a single &amp;quot;return point&amp;quot; for any label - if a ComeFrom call tries to register the &lt;em&gt;existing &lt;/em&gt;continuation point for a label, we ignore it; otherwise we throw an exception. So we only need to care about a single continuation for any label, which makes life easier.&lt;/p&gt;  &lt;p&gt;The second point is trickier. If you remember back to earlier posts in this series, we saw that the state machine generated for async only really contains a single entry point (MoveNext) which is used for &lt;em&gt;all&lt;/em&gt; continuations. A variable in the state machine is responsible for remembering where we were within it between calls. So in order to really make the continuation remember the point at which it needs to continue, we need to remember that state. We need to store an object for the continuation, which contains the delegate to invoke, and the state of the state machine when we were first passed the continuation. I&amp;#39;ve created a class for this, unimaginatively called Continuation, which looks like this:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="XmlComment"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span class="XmlComment"&gt;/// This hack allows a continuation to be executed more than once,&lt;/span&gt;     &lt;br /&gt;&lt;span class="XmlComment"&gt;/// contrary to the C# spec. It does this using reflection to store the&lt;/span&gt;     &lt;br /&gt;&lt;span class="XmlComment"&gt;/// value of the &amp;quot;state&amp;quot; field within the generated class. NEVER, EVER, EVER&lt;/span&gt;     &lt;br /&gt;&lt;span class="XmlComment"&gt;/// try to use this in real code. It&amp;#39;s purely for fun.&lt;/span&gt;     &lt;br /&gt;&lt;span class="XmlComment"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;     &lt;br /&gt;&lt;span class="Modifier"&gt;internal&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;sealed&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Continuation : IEquatable&amp;lt;Continuation&amp;gt;     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;int&lt;/span&gt; savedState;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;object&lt;/span&gt; target;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; FieldInfo field;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; Action action;     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;internal&lt;/span&gt; Continuation(Action action)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; target = action.Target;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; field = target.GetType().GetField(&lt;span class="String"&gt;&amp;quot;&amp;lt;&amp;gt;1__state&amp;quot;&lt;/span&gt;, BindingFlags.Instance | BindingFlags.NonPublic);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; savedState = (&lt;span class="ValueType"&gt;int&lt;/span&gt;) field.GetValue(target);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Keyword"&gt;this&lt;/span&gt;.action = action;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;internal&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; Execute()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; field.SetValue(target, savedState);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; action();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Snip Equals/GetHashCode&lt;/span&gt;     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;Yes, we use reflection to fish out the &amp;lt;&amp;gt;1__state variable initially, and poke the state machine with the same value when we next want to execute the continuation. All highly implementation-specific, of course.&lt;/p&gt;  &lt;p&gt;Now the ComeFrom method is reasonably straightforward - all we need is a dictionary mapping labels to continuations. Oh, and the same action stack shuffling as for Execute:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="InlineComment"&gt;// In the coordinator&lt;/span&gt;     &lt;br /&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; Dictionary&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;, Continuation&amp;gt; labels = &lt;span class="Keyword"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;, Continuation&amp;gt;();     &lt;br /&gt;    &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt; ComeFromAwaiter ComeFrom(&lt;span class="ReferenceType"&gt;string&lt;/span&gt; label)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; ComeFromAwaiter(label, &lt;span class="Keyword"&gt;this&lt;/span&gt;);     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;struct&lt;/span&gt; ComeFromAwaiter     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;string&lt;/span&gt; label;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; Coordinator coordinator;     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;internal&lt;/span&gt; ComeFromAwaiter(&lt;span class="ReferenceType"&gt;string&lt;/span&gt; label, Coordinator coordinator)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Keyword"&gt;this&lt;/span&gt;.label = label;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Keyword"&gt;this&lt;/span&gt;.coordinator = coordinator;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt; ComeFromAwaiter GetAwaiter()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;this&lt;/span&gt;;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// We *always* want to be given the continuation&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;bool&lt;/span&gt; IsCompleted { get { &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;false&lt;/span&gt;; } }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; OnCompleted(Action action)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Continuation newContinuation = &lt;span class="Keyword"&gt;new&lt;/span&gt; Continuation(action);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Continuation oldContinuation;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;if&lt;/span&gt; (!coordinator.labels.TryGetValue(label, &lt;span class="MethodParameter"&gt;out&lt;/span&gt; oldContinuation))     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// First time coming from this label. Always succeeds.&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; coordinator.labels[label] = newContinuation;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;else&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Current semantics are to prohibit two different ComeFrom calls for the same label.&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// An alternative would be to just replace the existing continuation with the new one,&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// in which case we wouldn&amp;#39;t need any of this - we could just use&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// coordinator.labels[label] = newContinuation;&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// unconditionally.&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;if&lt;/span&gt; (!oldContinuation.Equals(newContinuation))     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;throw&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; InvalidOperationException(&lt;span class="String"&gt;&amp;quot;Additional continuation detected for label &amp;quot;&lt;/span&gt; + label);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Okay, we&amp;#39;ve seen this one before. Nothing to see here, move on.&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// We actually want to continue from where we were: we&amp;#39;re only really marking the&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// ComeFrom point.&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; coordinator.stack.Push(action);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; GetResult()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;There&amp;#39;s one interesting point here which is somewhat subtle, and screwed me up for a bit...&lt;/p&gt;  &lt;h3&gt;The default value of a struct is always valid...&lt;/h3&gt;  &lt;p&gt;You may have noticed that ComeFromAwaiter is a struct. That&amp;#39;s pretty unusual for me. However, it&amp;#39;s also &lt;em&gt;absolutely critical&lt;/em&gt;. Without it, we&amp;#39;d get a NullReferenceException when we execute the continuation the second time.&lt;/p&gt;  &lt;p&gt;Normally, the flow of async methods looks a bit like this, for an await expression taking the &amp;quot;long&amp;quot; route (i.e. IsCompleted is false):&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Call GetAwaiter() and assign the result to an awaiter field &lt;/li&gt;    &lt;li&gt;Call IsCompleted (which returns false in this scenario) &lt;/li&gt;    &lt;li&gt;Set the state variable to remember where we&amp;#39;d got to &lt;/li&gt;    &lt;li&gt;Call OnCompleted &lt;/li&gt;    &lt;li&gt;Return &lt;/li&gt;    &lt;li&gt;... When we continue... &lt;/li&gt;    &lt;li&gt;Set state to 0 (running) &lt;/li&gt;    &lt;li&gt;Call GetResult() on the awaiter &lt;/li&gt;    &lt;li&gt;Set the awaiter field to default(&lt;em&gt;TypeOfAwaiter&lt;/em&gt;) &lt;/li&gt;    &lt;li&gt;Continue &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Now that&amp;#39;s fine when we&amp;#39;re only continuing &lt;em&gt;once&lt;/em&gt; - but if we need to jump into the middle of that sequence a second time, we&amp;#39;re going to call GetAwaiter() on the awaiter field &lt;em&gt;after it&amp;#39;s been set to the default value of the awaiter type&lt;/em&gt;. If the default value is null, we&amp;#39;ll go bang. So we &lt;em&gt;must&lt;/em&gt; use a struct.&lt;/p&gt;  &lt;p&gt;Fortunately, our GetResult() call doesn&amp;#39;t need any of the state in the awaiter - it&amp;#39;s &lt;em&gt;purely&lt;/em&gt; there to satisfy the normal flow of things. So we&amp;#39;re quite happy with a &amp;quot;default&amp;quot; ComeFrom awaiter.&lt;/p&gt;  &lt;h3&gt;Finally, labels...&lt;/h3&gt;  &lt;p&gt;We&amp;#39;ve now done all the hard work. The final piece of the puzzle is Label, which &lt;em&gt;just&lt;/em&gt; needs to check whether there&amp;#39;s a continuation to jump to, and shuffle the action stack in the way we&amp;#39;re now painfully accustomed to:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt; LabelAwaiter Label(&lt;span class="ReferenceType"&gt;string&lt;/span&gt; label)    &lt;br /&gt;{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Continuation continuation;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; labels.TryGetValue(label, &lt;span class="MethodParameter"&gt;out&lt;/span&gt; continuation);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; LabelAwaiter(continuation, &lt;span class="Keyword"&gt;this&lt;/span&gt;);    &lt;br /&gt;}    &lt;br /&gt;    &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; LabelAwaiter    &lt;br /&gt;{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; Continuation continuation;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; Coordinator coordinator;    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;internal&lt;/span&gt; LabelAwaiter(Continuation continuation, Coordinator coordinator)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Keyword"&gt;this&lt;/span&gt;.continuation = continuation;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Keyword"&gt;this&lt;/span&gt;.coordinator = coordinator;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt; LabelAwaiter GetAwaiter()    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;this&lt;/span&gt;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// If there&amp;#39;s no continuation to execute, just breeze through.&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;bool&lt;/span&gt; IsCompleted { get { &lt;span class="Statement"&gt;return&lt;/span&gt; continuation == &lt;span class="Keyword"&gt;null&lt;/span&gt;; } }    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; OnCompleted(Action action)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// We want to execute the ComeFrom continuation, then get back here.&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; coordinator.stack.Push(action);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; coordinator.stack.Push(continuation.Execute);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; GetResult()    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;Almost painfully simple, really.&lt;/p&gt;  &lt;p&gt;So that &lt;em&gt;looks&lt;/em&gt; like all the code that&amp;#39;s used, right? Not &lt;em&gt;quite&lt;/em&gt;...&lt;/p&gt;  &lt;h3&gt;Reusable builders?&lt;/h3&gt;  &lt;p&gt;As we saw in the sample code, we can end up finishing the same async method multiple times (SimpleOtherMethod completes three times). That&amp;#39;s going to call SetResult on the AsyncVoidMethodBuilder three times... which feels like it &lt;em&gt;should&lt;/em&gt; go bang. Indeed, when I revisited my code earlier I wondered why it &lt;em&gt;didn&amp;#39;t&lt;/em&gt; go bang - it&amp;#39;s the sort of illegal state transition the framework is usually pretty good at picking up on.&lt;/p&gt;  &lt;p&gt;Then I remembered - this isn&amp;#39;t the framework&amp;#39;s AsyncVoidMethodBuilder - it&amp;#39;s mine. And my SetResult method in this project does &lt;em&gt;absolutely nothing&lt;/em&gt;. How convenient!&lt;/p&gt;  &lt;h2&gt;Make it stop, make it stop!&lt;/h2&gt;  &lt;p&gt;Okay thiat was a pretty quick tour of some horrible code. You&amp;#39;ll never have to do anything like this with async in sane code, but it certainly made me painfully familiar with how it all worked. Just to recap on the oddities involved:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;We needed to capture a continuation and then immediately keep going, &lt;em&gt;almost&lt;/em&gt; as if the awaiter had said the awaitable had completed already. This involved shenanigans with the execution model and an extra method (Execute)&lt;/li&gt;    &lt;li&gt;We needed to remember the state of a continuation, which we did with reflection.&lt;/li&gt;    &lt;li&gt;We needed to make awaiter.GetResult() a valid call after awaiter had been reset to the default value for the type&lt;/li&gt;    &lt;li&gt;We needed to ensure that the builder created in the skeleton method could have SetResult called on it multiple times&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;That&amp;#39;s all on continuations and co-routines, I promise.&lt;/p&gt;  &lt;p&gt;Next time (hopefully soon) I&amp;#39;ll look at an example of how composition works so neatly in async, and then show how we can unit test async methods - at least sometimes.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1802363" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/RPulVO5m5sM" height="1" width="1"/&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Evil+Code/default.aspx">Evil Code</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+5/default.aspx">C# 5</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Eduasync/default.aspx">Eduasync</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2011/11/11/eduasync-part-15-implementing-comefrom-with-a-horrible-hack.aspx</feedburner:origLink></item><item><title>Laptop review: Kobalt G150</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/nsJmZkaZ39o/laptop-review-kobalt-g150.aspx</link><pubDate>Sun, 18 Sep 2011 07:09:41 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1799728</guid><dc:creator>skeet</dc:creator><slash:comments>17</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1799728</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1799728</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2011/09/18/laptop-review-kobalt-g150.aspx#comments</comments><description>&lt;p&gt;&lt;strong&gt;&lt;font color="#ff0000"&gt;EDIT, 17th October 2011&lt;/font&gt;&lt;/strong&gt;: Last week Kobalt closed down... so the choice about whether or not I&amp;#39;d buy from them again is now moot. However, &lt;a href="http://pcspecialist.co.uk"&gt;PC Specialist&lt;/a&gt; sells a very similar spec, now including the matte screen...&lt;/p&gt;  &lt;p&gt;As some of you will know, our house was burgled in April, and the thieves took three laptops (and very little else), including my main personal laptop. Obviously I ordered a replacement, partly covered by the insurance from my old laptop. However, I took the opportunity to spoil myself a little... I ordered a G150 from &lt;a href="http://www.kobaltcomputers.co.uk/"&gt;Kobalt Computers&lt;/a&gt;. Various people have taken an interest in the progress and results of this, so this post is a little review.&lt;/p&gt;  &lt;h3&gt;Specs&lt;/h3&gt;  &lt;p&gt;As I said, I spoiled myself a little... the specs are somewhat silly:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Overall, it&amp;#39;s a G150 which is based on the &lt;a href="http://www.clevo.com.tw/en/products/prodinfo.asp?productid=307"&gt;Clevo P150HM&lt;/a&gt; chassis &lt;/li&gt;    &lt;li&gt;Screen: 1920x1080, matte, 95% gamut &lt;/li&gt;    &lt;li&gt;CPU: Intel Sandybridge Core-i7 2720QM; quad core 2.2-3.33GHz &lt;/li&gt;    &lt;li&gt;Graphics: &lt;a href="http://www.notebookcheck.net/NVIDIA-GeForce-GTX-580M.56636.0.html"&gt;Nvidia GeForce GTX 580M&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;RAM: 16GB Corsair DDR3 &lt;/li&gt;    &lt;li&gt;Disk: SSD - Intel 510, 250GB &lt;/li&gt;    &lt;li&gt;Optical: Blu-ray ROM; DVD/RW &lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;How does it run?&lt;/h3&gt;  &lt;p&gt;Well how do you &lt;em&gt;think&lt;/em&gt; it runs? :) Obviously it&amp;#39;s very nippy indeed. I&amp;#39;m not sure I&amp;#39;ve ever used more than a couple of cores at a time yet, but it&amp;#39;s nice to know I &lt;em&gt;can&lt;/em&gt; test out parallelization when I want to :) While Windows Experience numbers are obviously pretty crude, they&amp;#39;re pleasant enough that I might as well show them off:&lt;/p&gt;  &lt;p&gt;&lt;img style="margin:5px 5px 5px 0px;" src="http://yoda.arachsys.com/csharp/blogfiles/ExperienceIndex.png" alt="" /&gt;&lt;/p&gt;  &lt;p&gt;Visual Studio 2010 still takes a while to come up (10-15 seconds) whereas Office components start pretty much instantly - so I don&amp;#39;t know where the bottleneck for VS is. You can do an awful lot in terms of both disk and CPU in 10 seconds on this laptop, so it&amp;#39;s a bit of a mystery. (Eclipse starts noticeably faster.) However, once running, Visual Studio is as fast as you&amp;#39;d expect on this machine - builds and tests are nippy, and the editor is noticeably more responsive than on the &amp;quot;make-do&amp;quot; laptop I&amp;#39;ve been using since April.&lt;/p&gt;  &lt;p&gt;I can&amp;#39;t say I&amp;#39;m much of a gamer, but my experiences in Call Of Duty: Black Ops and Portal 2 have been wonderful so far; I can put everything on high settings until it looks absolutely beautiful, and still get a good frame rate. I&amp;#39;ve never had a laptop with a really good graphics card before, and the one in this beast is one of the most powerful out there, so I&amp;#39;ve really got no excuse for &lt;em&gt;not&lt;/em&gt; getting into gaming more, other than my obvious lack of free time. I&amp;#39;m also looking forward to investigating the possibility of writing code in C# to be executed on the GPU - I believe there are a couple of projects around that, so it&amp;#39;ll be fun to look into.&lt;/p&gt;  &lt;p&gt;With a fast SSD, the boot time is fabulously fast - although it &lt;em&gt;does&lt;/em&gt; take a while to go to sleep, as I use hybrid sleep mode and it needs to dump memory to disk first. That&amp;#39;s one downside of having a large amount of memory, of course. There&amp;#39;s still a lot of debate around the longevity of solid state drives, but the improved performance is &lt;em&gt;so&lt;/em&gt; noticeable that I&amp;#39;d definitely not go back to a &amp;quot;normal&amp;quot; hard drive now. I chose the Intel 510 over some slightly faster drives as the 510 is generally reckoned to be more reliable - so I&amp;#39;m hedging my bets somewhat. I suspect the difference in performance between &amp;quot;stupidly fast&amp;quot; and &amp;quot;ridiculously fast&amp;quot; is irrelevant to me.&lt;/p&gt;  &lt;p&gt;The screen is beautiful - just &amp;quot;really nice&amp;quot; for normal desktop work, but &lt;em&gt;amazing&lt;/em&gt; for games and video - that&amp;#39;s where the matte nature really wins out. The contrast is particularly nice, at least in the short tests I&amp;#39;ve performed so far. The hinge feels pleasantly firm but not stiff - it&amp;#39;s hard to judge this early, but hopefully it&amp;#39;ll prove robust over time. This screen is one of the reasons I chose Kobalt - I believe they&amp;#39;re the only UK company selling machines with this screen, and I&amp;#39;d certainly recommend that anyone who has the chance to go for the matte screen should do so.&lt;/p&gt;  &lt;p&gt;Personally I use a separate keyboard most of the time (see below) but the keyboard on the G150 itself is a nice chiclet style one. I&amp;#39;m not terribly fond of the layout of the cursor keys of the fact that there&amp;#39;s no separate home / end / page up / page down keys, but it&amp;#39;s not a big deal. The feeling of the keys themselves is good, and not too loud. The trackpad is fine - I&amp;#39;ve turned off &amp;quot;tap to click&amp;quot; as it always ends up activating when I don&amp;#39;t want to, but that&amp;#39;s not specific to this particular laptop - I &lt;em&gt;always&lt;/em&gt; find the same thing. Maybe I type with my palms particularly close to the trackpad, or something like that.&lt;/p&gt;  &lt;p&gt;A few more random, esoteric points:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The power socket is &amp;quot;grippy&amp;quot; which makes it slightly harder to pull out the power, but does give a feeling of security. This is clearly a deliberate decision, and while it&amp;#39;s not one which would suit &lt;em&gt;everyone&lt;/em&gt;, I&amp;#39;m pretty happy with it. &lt;/li&gt;    &lt;li&gt;The fan comes on and goes off reasonably regularly, which can prove a little distracting sometimes, but is the natural result of having a fast/hot laptop, I guess. The fan itself is fairly quiet under normal load, so I&amp;#39;d probably be fine with it being &lt;em&gt;constantly&lt;/em&gt; on - it&amp;#39;s the stop / start nature that jars a little. Not a big deal once you get used to it. &lt;/li&gt;    &lt;li&gt;The webcam is really washed out - I don&amp;#39;t think I&amp;#39;d really want to use it, to be honest. I don&amp;#39;t know whether it&amp;#39;s my particular hardware, the general make/model, or the settings (which I&amp;#39;ve played with and improved &lt;em&gt;somewhat&lt;/em&gt;, but not to really acceptable levels) &lt;/li&gt;    &lt;li&gt;The built-in microphone is located on the keyboard, which is a little bizarre and obviously not helpful for any time you&amp;#39;d be typing as well as talking. There&amp;#39;s a lot of white noise with it compared with actual signal - I couldn&amp;#39;t get a Skype test call to be audible without it also having huge amounts of hiss. Fortunately, this isn&amp;#39;t a problem for me - I have a standalone microphone which I use for screencasts etc. That&amp;#39;s previously been problematically quiet with other laptops, possibly due to drawing a lot of power - but it works perfectly with this laptop. I&amp;#39;m also getting a Corsair headset, which should be handy both for gaming and podcast recordings. &lt;/li&gt;    &lt;li&gt;The machine itself is fairly bulky, and the power brick is huge - but the feeling of the chassis is a very attractive matte black. If you&amp;#39;re looking for a sleek laptop to carry around a lot, this probably wouldn&amp;#39;t be the best choice, but most of the time I keep my laptop on the same table at home. I went for a 15&amp;quot; rather than 17&amp;quot; as it makes a big difference when carrying it around to conferences, but the extra thickness required to house and cool the powerful components doesn&amp;#39;t really bother me. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Overall, I&amp;#39;m really pleased with the laptop. As far as I can tell so far, the build quality is excellent - no problems at all. The poor quality of the microphone and webcam are a niggly disappointment, but not one that bothers me enough to find out whether they&amp;#39;re &amp;quot;working as expected&amp;quot; or not.&lt;/p&gt;  &lt;h3&gt;The buying experience&lt;/h3&gt;  &lt;p&gt;This is where some of you may be relishing the prospect of reading a rant against Kobalt - but I&amp;#39;m not going to air lots of dirty laundry here. It &lt;em&gt;did&lt;/em&gt; take a very long time for the laptop to arrive - over three months - but the causes of this were varied, and are worth mentioning at least in passing. I&amp;#39;m hoping that a summary of frustrations and the good parts will give the right impression without getting ugly.&lt;/p&gt;  &lt;p&gt;My first cause of frustration occurred very early - before I&amp;#39;d even received a detailed order confirmation from Kobalt. They&amp;#39;d suffered from a high proportion of staff going down with norovirus around the time I ordered. This is the sort of thing which will obviously hit a small company like Kobalt rather more than bigger ones, but it wasn&amp;#39;t a great start; for a week all I had was an email saying that I&amp;#39;d paid Kobalt for &lt;em&gt;something&lt;/em&gt;, but I couldn&amp;#39;t even check whether the details were right.&lt;/p&gt;  &lt;p&gt;Some of the delays actually put Kobalt into a &lt;em&gt;better&lt;/em&gt; light as far as I&amp;#39;m concerned. In particular, I&amp;#39;d originally ordered an AMD 6970 graphics card and a Vertex 3 SSD. Kobalt withdrew both of these components from sale: the 6970 was failing too often in testing, and the Vertex 3 was causing blue screens, sometimes in testing and occasionally on customer machines, due to an issue between the laptop chipset and the disk. Some other vendors would no doubt have kept selling these components and let the customer take the risk of losing the use of the laptop while it went back for repair, and I applaud Kobalt for &lt;em&gt;not&lt;/em&gt; doing this.&lt;/p&gt;  &lt;p&gt;Likewise my order was actually built twice: the first one failed testing - the motherboard failed, so had to be built from scratch. Again, I&amp;#39;m very happy about this - I&amp;#39;d obviously rather have a delay but get a working laptop in the end than get a defective one sooner. This also worked to my advantage in terms of the graphics card; although I&amp;#39;d only ordered a 485M, the first one was used in another customer&amp;#39;s machine while waiting for a new motherboard for me... by which time the 485M was no longer readily available. Kobalt swapped in the 580M for no extra charge.&lt;/p&gt;  &lt;p&gt;Other delays were only partially under Kobalt&amp;#39;s control - for example, their order management system blew up in August. Can you blame a company for their internal systems failing? It&amp;#39;s hard to say - and I&amp;#39;ll be the first to admit I don&amp;#39;t have the details. Was it due to cutting corners by getting a cheap ordering system which might be expected to be flaky? Was it due to something catastrophic which would only happen once in a million years? Should there have been better &amp;quot;emergency backup&amp;quot; procedures? I don&amp;#39;t know - but it &lt;em&gt;feels&lt;/em&gt; like something that customers shouldn&amp;#39;t be exposed to.&lt;/p&gt;  &lt;p&gt;One benefit of the delays was that I was able to change my order a couple of times - upping the CPU and memory, choosing a different graphics card and disk drive etc. Kobalt have been very flexible around this; it was considerably easier to change the configuration than I suspect it would have been with somewhere like Dell.&lt;/p&gt;  &lt;p&gt;My main issue through all of this was communication - which wasn&amp;#39;t all bad, but was definitely flaky. I suspect Kobalt would argue that I had unreasonable expectations, whereas I&amp;#39;d say it&amp;#39;s just part of providing good customer service. The problems that Kobalt experienced obviously made all of this &lt;em&gt;much&lt;/em&gt; worse than normal, but I still think there&amp;#39;s a mismatched set of expectations there. I clearly wasn&amp;#39;t the only one getting frustrated - the forums had a lot of posts complaining of a lack of updates - but equally, satisfied customers don&amp;#39;t tend to post much to provide balance. It&amp;#39;s very obvious on the forums that while people &lt;em&gt;have&lt;/em&gt; been frustrated with the buying process, almost everyone&amp;#39;s really happy with the machine they get in the end, and Kobalt are actually really good at answering questions about drivers etc afterwards.&lt;/p&gt;  &lt;p&gt;My own experience of communicating with Kobalt was negative until the reasonably late stages of the order, but I was then phoned to be informed of the laptop coming out of testing and again to check shipping dates, which was good. (In this case it was particularly important as without the check, the laptop would have been delivered to the office on Saturday, where there may well not have been anyone prepared to sign for it.)&lt;/p&gt;  &lt;p&gt;For a week or so after I received my laptop I still saw quite a few frustrated posts from other customers, but now that &lt;em&gt;seems&lt;/em&gt; to have gone down significantly. It&amp;#39;s possible that I&amp;#39;m just not seeing the other complaining posts (as such posts are often deleted - leading to the customer in question getting &lt;em&gt;more&lt;/em&gt; frustrated, of course). I&amp;#39;m currently hopeful that I happen to have just ordered at a really bad time where Kobalt was suffering a series of unrelated problems. Whether they handled those as well as they could have done is up for debate, but &lt;em&gt;hopefully&lt;/em&gt; new customers wouldn&amp;#39;t see the same problems.&lt;/p&gt;  &lt;p&gt;It should be noted that Kobalt is definitely trying to get better, too. In August they introduced a new &lt;a href="http://www.kobaltcomputers.co.uk/forum/showthread.php?3918-New!!-Kobalt-Customer-Promise"&gt;Customer Promise&lt;/a&gt; around price, upgrades, and delivery times. In particular, if your order takes more than 6 weeks, you can choose between various games and accessories as a gift. (I chose two games from Steam.) Also, they&amp;#39;re actively recruiting, so hopefully that will help on the communications front, too.&lt;/p&gt;  &lt;h3&gt;Accessories&lt;/h3&gt;  &lt;p&gt;As tends to happen while waiting for something, I got itchy and started buying accessories to go with the new laptop. I thought I might as well mention those at the same time...&lt;/p&gt;  &lt;p&gt;External USB 3 drive: &lt;a href="http://www.amazon.co.uk/dp/B002NEFU9M"&gt;Western Digital MyPassport&lt;/a&gt;. I would probably have bought a eSata drive if I&amp;#39;d found one with as neat a form factor as this, but there just don&amp;#39;t seem to be many eSata drives around yet. Hopefully this will change, as I &lt;em&gt;do&lt;/em&gt; notice my USB mouse/keyboard responding sluggishly while I&amp;#39;m putting a lot of traffic through the drive - but apart from that, it&amp;#39;s lovely. It&amp;#39;s a really nice form factor, and seems to take advantage of the USB 3 ports on my laptop to deliver pretty reasonable performance. I&amp;#39;m expecting this to be used primarily for VMs - I&amp;#39;ve heard mixed reports of using VMs with SSDs, including the possibility that the two really don&amp;#39;t play nicely, leading to early drive failure. I don&amp;#39;t know whether this is accurate or not, but I&amp;#39;m being cautious. Overall, I&amp;#39;m pretty happy with this, although it&amp;#39;s reasonably hard to get excited about a disk drive...&lt;/p&gt;  &lt;p&gt;Keyboard: I do quite a bit of typing, and while I&amp;#39;m used to laptop keyboards, they&amp;#39;re obviously somewhat constrained. I had previously been using a &lt;a href="http://www.logitech.com/en-gb/keyboards/keyboard/devices/6007"&gt;Logitech K340&lt;/a&gt; which is nice, but I treated myself to a &lt;a href="http://www.logitech.com/en-gb/keyboards/keyboard/devices/7288"&gt;K800&lt;/a&gt; for the new machine. Both of these use Logitech&amp;#39;s &amp;quot;Unifying&amp;quot; receiver, which is &lt;em&gt;wonderful&lt;/em&gt; - it&amp;#39;s a tiny little receiver which I just leave permanently in the USB port. It works with quite a few Logitech peripherals, so I share the same receiver for my keyboard and the &lt;a href="http://www.logitech.com/en-gb/mice-pointers/mice/devices/5846"&gt;Anywhere MX&lt;/a&gt; mouse I use. The K800 keyboard is really nice - a lovely action, a sensible layout of Ins/Del/Home/End/PgUp/PgDn (which is its main benefit over the K340, to be honest) and it&amp;#39;s rechargable via USB. The backlighting is a nice extra, although it&amp;#39;s probably not going to actually be &lt;em&gt;useful&lt;/em&gt; for me. It&amp;#39;s fun to just wave your hand over it and watch it light up though... I&amp;#39;m easily pleased :)&lt;/p&gt;  &lt;p&gt;I also bought a &lt;a href="http://www.amazon.co.uk/dp/B005626UPG"&gt;Belkin Screencast WiDi 2.0&lt;/a&gt;, which turns out to have been a mistake. I had &lt;em&gt;thought&lt;/em&gt; that because I was using a Sandybridge laptop with an appropriate Intel wifi card, I&amp;#39;d be able to use WiDi - a technology which allows you to transmit video and sound to a receiver which can then plug into the TV. Yay, I can display Youtube etc on the TV without leaving the comfort of the sofa, right? Not so much - it turns out that this &lt;em&gt;only&lt;/em&gt; works if you&amp;#39;re also using the integrated graphics card; as I&amp;#39;m using a separate GPU, it&amp;#39;s a no-go. This wasn&amp;#39;t made as obvious as it might be on Intel&amp;#39;s web site about WiDi - it&amp;#39;s there if you dig, but it&amp;#39;s not obvious. Just to be clear, this is in &lt;em&gt;no&lt;/em&gt; way Kobalt&amp;#39;s fault - they never claimed the new laptop would be WiDi compatible. I&amp;#39;ve now sent the Screencast (which was no use to me for anything else) to Kobalt so they can try it out with other laptops. (I suspect the GS150 may work with it, for example.)&lt;/p&gt;  &lt;h3&gt;VM experiment&lt;/h3&gt;  &lt;p&gt;As I&amp;#39;ve tweeted before, I did have one hope for actually using most of the 16GB of memory. I don&amp;#39;t want to run VMs directly from the SSD, as I mentioned before - but I had a thought of having the virtual disk &lt;em&gt;on&lt;/em&gt; the SSD, but then mounting it as a ram drive. That way I&amp;#39;d only need to write to the disk after shutting down the VM - one big write instead of frequent spraying access.&lt;/p&gt;  &lt;p&gt;That would only work with a small drive, of course... but I &lt;em&gt;hoped&lt;/em&gt; I&amp;#39;d just about be able to get Windows 7 Home Premium + Visual Studio into a small enough drive. With 1GB of memory for the VM and 2GB of memory for the host machine I can have a 13GB ram drive - and I can install Windows 7 on that using Virtual PC, but Virtual PC also uses disk space alongside the VHD for memory for the VM, which obviously takes another 1GB off the usable size. I &lt;em&gt;nearly&lt;/em&gt; managed it, but not quite. I may give it another go with Virtual Box and the Express edition of VS11... I&amp;#39;ll blog again if I get it working, but I didn&amp;#39;t want to hold up this post forever...&lt;/p&gt;  &lt;p&gt;In terms of getting &lt;em&gt;anything&lt;/em&gt; working, it took a little while - DataRam&amp;#39;s &lt;a href="http://memory.dataram.com/products-and-services/software/ramdisk"&gt;RAMDisk product&lt;/a&gt; kept hanging while closing down; &lt;a href="http://www.ltr-data.se/opencode.html/#ImDisk"&gt;imdisk&lt;/a&gt; gave me access problems even after trying all the suggested tweaks, but &lt;a href="http://www.romexsoftware.com/en-us/index.html"&gt;VSuite Ramdisk (server edition)&lt;/a&gt; seems to do the job. It&amp;#39;s not hugely cheap (and I need the server edition to support a drive over 4GB) but &lt;em&gt;if&lt;/em&gt; I can get everything working, I may go with it. Currently I&amp;#39;m using the trial edition.&lt;/p&gt;  &lt;h3&gt;Conclusion&lt;/h3&gt;  &lt;p&gt;I guess the obvious question to ask is &amp;quot;If I had my time again, would I take the same action?&amp;quot;&lt;/p&gt;  &lt;p&gt;Well, it&amp;#39;s obviously been a frustrating experience, but the results should keep me happy for a long time. I think I would be &lt;em&gt;cautious&lt;/em&gt; about buying from Kobalt again, but probably less so than you might expect. I&amp;#39;d probably hang out in the forums for a while to see whether folks were generally happy at the time. I&amp;#39;m &lt;em&gt;hoping&lt;/em&gt; I was just unlucky, and hit a particularly nasty time in Kobalt&amp;#39;s history - I can&amp;#39;t imagine the staff there have enjoyed those three months any more than I did - and that normally everything runs smoothly. If I were in a real hurry I&amp;#39;d probably go for an off-the-shelf solution, but that&amp;#39;s a different matter - when you buy a custom machine you should &lt;em&gt;expect&lt;/em&gt; it to take a bit longer. Just not three months, normally :)&lt;/p&gt;  &lt;p&gt;I&amp;#39;d certainly be happy to buy from Kobalt again in terms of the quality of the product - it&amp;#39;s a &lt;em&gt;lovely&lt;/em&gt; laptop, and I&amp;#39;m delighted with its performance, display and general handling. Obviously I regret buying the Screencast, but all my other decisions - memory, disk, external keyboard etc - have turned out well so far.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1799728" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/nsJmZkaZ39o" height="1" width="1"/&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/General/default.aspx">General</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2011/09/18/laptop-review-kobalt-g150.aspx</feedburner:origLink></item><item><title>Upcoming speaking engagements</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/ZKs8HsqQYw4/upcoming-speaking-engagements.aspx</link><pubDate>Fri, 02 Sep 2011 16:53:17 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1798771</guid><dc:creator>skeet</dc:creator><slash:comments>8</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1798771</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1798771</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2011/09/02/upcoming-speaking-engagements.aspx#comments</comments><description>&lt;p&gt;It&amp;#39;s just occurred to me that I&amp;#39;ve forgotten to mention a few of the things I&amp;#39;ll be up to in the near-ish future. (I&amp;#39;ve &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2011/07/28/speaking-engagement-progressive-net-london-september-7th.aspx"&gt;talked about next week&amp;#39;s Progressive .NET session before&lt;/a&gt;.) This is just a quick rundown - follow the links for more blurb and details.&lt;/p&gt;  &lt;h2&gt;.NET Developer Network - Bristol, September 21st (evening)&lt;/h2&gt;  &lt;p&gt;I&amp;#39;ll be &lt;a href="http://dotnetdevnet.com/Meetings/tabid/54/EntryID/58/Default.aspx"&gt;talking about async&lt;/a&gt; in Bristol - possibly at a high level, possibly in detail, depending on the audience experience. This is my first time talking with this particular user group, although I&amp;#39;m sure there&amp;#39;ll be some familiar faces. Come along if you&amp;#39;re in the area.&lt;/p&gt;  &lt;h2&gt;Øredev 2011 - Malmö, November 9th&lt;/h2&gt;  &lt;p&gt;It&amp;#39;s a whistle-stop trip to Sweden as I&amp;#39;m running out of vacation days; I&amp;#39;m flying out on the Tuesday evening and back on the Wednesday evening, but while I&amp;#39;m there I&amp;#39;ll give two talks:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://oredev.org/2011/sessions/async-101"&gt;Async 101&lt;/a&gt; (yes, &lt;em&gt;more&lt;/em&gt; async; I wonder at what point I&amp;#39;ll have given as many talks about it as Mads) &lt;/li&gt;    &lt;li&gt;&lt;a href="http://oredev.org/2011/sessions/a-less-technical-talk-on-technical-communication"&gt;Effective technical communication&lt;/a&gt; (not a particularly technical talk, but definitely specific to &lt;em&gt;technical&lt;/em&gt; communication) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Last year I had an absolute blast - looking forward to this year, even though I won&amp;#39;t have as much time for socializing.&lt;/p&gt;  &lt;h2&gt;Stack Overflow Dev Days 2011 - London, November 14th - cancelled!&lt;/h2&gt;  &lt;p&gt;&lt;strong&gt;Update: &lt;a href="http://blog.stackoverflow.com/2011/09/devdays-2011-is-cancelled/"&gt;Dev Days has been cancelled&lt;/a&gt;. I&amp;#39;m still hoping to do &lt;em&gt;something&lt;/em&gt; around this topic, and there may be small-scale meet-ups in London anyway.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Two years ago I talked about &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2009/11/02/omg-ponies-aka-humanity-epic-fail.aspx"&gt;how humanity had let the world of software engineering down&lt;/a&gt;. This was one of the best talks I&amp;#39;ve ever given, and introduced the world to Tony the Pony. Unfortunately that puts the bar relatively high for this year&amp;#39;s talk - at least, high by my own pretty low standards.&lt;/p&gt;  &lt;p&gt;In a somewhat odd topic for a &lt;a href="http://pobox.com/~skeet/preaching"&gt;Christian&lt;/a&gt; and a happy employee of a company with a &lt;a href="http://investor.google.com/corporate/code-of-conduct.html"&gt;code of conduct&lt;/a&gt; which starts &amp;quot;Don&amp;#39;t be evil,&amp;quot; this year&amp;#39;s talk is entitled &lt;a href="http://devdays.stackoverflow.com/sessions/thinking-in-evil/"&gt;&amp;quot;Thinking in evil.&amp;quot;&lt;/a&gt; As regular readers are no doubt aware, I love torturing the C# language and forcing the compiler to work with code which would make any right-thinking software engineer cringe. I was particularly gratified recently when Eric Lippert commented on &lt;a href="http://stackoverflow.com/questions/7113347/c-assignment-in-an-if-statement/7113387#7113387"&gt;one of my Stack Overflow answers&lt;/a&gt; that this was &amp;quot;the best abuse of C# I&amp;#39;ve seen in a while.&amp;quot; I&amp;#39;m looking forward to talking about why I think it&amp;#39;s genuinely a good idea to think about nasty code like this - not to &lt;em&gt;use&lt;/em&gt; it, but to get to know your language of choice more intimately. Like last time, I have little idea of &lt;em&gt;exactly&lt;/em&gt; what this talk will be like, but I&amp;#39;m really looking forward to it.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1798771" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/ZKs8HsqQYw4" height="1" width="1"/&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Speaking+engagements/default.aspx">Speaking engagements</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Stack+Overflow/default.aspx">Stack Overflow</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Evil+Code/default.aspx">Evil Code</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2011/09/02/upcoming-speaking-engagements.aspx</feedburner:origLink></item><item><title>Optimization and generics, part 2: lambda expressions and reference types</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/cXsL5tTiNHw/optimization-and-generics-part-2-lambda-expressions-and-reference-types.aspx</link><pubDate>Mon, 22 Aug 2011 23:06:18 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1798036</guid><dc:creator>skeet</dc:creator><slash:comments>17</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1798036</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1798036</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2011/08/23/optimization-and-generics-part-2-lambda-expressions-and-reference-types.aspx#comments</comments><description>&lt;p&gt;&lt;strong&gt;As with almost any performance work, your mileage may vary (in particular the 64-bit JIT may work differently) and you almost certainly shouldn&amp;#39;t care. Relatively few people write production code which is worth micro-optimizing. Please don&amp;#39;t take this post as an invitation to make code more complicated for the sake of irrelevant and possibly mythical performance changes.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;It took me a surprisingly long time to find the problem described in the &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2011/08/22/optimization-and-generics-part-1-the-new-constraint.aspx"&gt;previous blog post&lt;/a&gt;, and almost no time at all to fix it. I understood why it was happening. This next problem took a while to identify at all, but even when I&amp;#39;d found a workaround I had no idea why it worked. Furthermore, I couldn&amp;#39;t reproduce it in a test case... because I was looking for the wrong set of triggers. I&amp;#39;ve now found at least &lt;em&gt;some&lt;/em&gt; of the problem though.&lt;/p&gt;  &lt;p&gt;This time the situation in Noda Time is harder to describe, although it concerns the same area of code. In various places I need to create new delegates containing parsing steps and add them to the list of steps required for a full parse. I can always use lambda expressions, but in many cases I&amp;#39;ve got the same logic repeatedly... so I decided to pull it out into a method. Bang - suddenly the code runs far slower. (In reality, I&amp;#39;d performed this refactoring first, and &amp;quot;unrefactored&amp;quot; it to speed things up.)&lt;/p&gt;  &lt;p&gt;I &lt;em&gt;think&lt;/em&gt; the problem comes down to method group conversions with generic methods and a type argument which is a reference type. The CLR isn&amp;#39;t very good at them, and the C# compiler uses them more than it needs to.&lt;/p&gt;  &lt;h3&gt;Show me the benchmark!&lt;/h3&gt;  &lt;p&gt;The &lt;a href="http://pobox.com/~skeet/csharp/blogfiles/GenericsAndLambdas.cs"&gt;complete benchmark code&lt;/a&gt; is available of course, but fundamentally I&amp;#39;m doing the same thing in each test case: creating a delegate of type Action which does nothing, and then checking that the delegate reference is non-null (just to avoid the JIT optimizing it away). In each case this is done in a generic method with a single type parameter. I call each method in two ways: once with int as the type argument, and once with string as the type argument. Here are the different cases involved:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Use a lambda expression: &lt;font face="Courier New"&gt;&lt;strong&gt;Action foo = () =&amp;gt; {};&lt;/strong&gt;&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;Fake what I &lt;em&gt;expected&lt;/em&gt; the compiler to do: keep a separate generic cache class with a static variable for the delegate; populate the cache once if necessary, and thereafter use the cache field &lt;/li&gt;    &lt;li&gt;Fake what the compiler is &lt;em&gt;actually&lt;/em&gt; doing with the lambda expression: write a separate generic method and perform a method group conversion to it &lt;/li&gt;    &lt;li&gt;Do what the compiler &lt;em&gt;could&lt;/em&gt; do: write a separate non-generic method and perform a method group conversion to it &lt;/li&gt;    &lt;li&gt;Use a method group conversion to a static (non-generic) method on a generic type &lt;/li&gt;    &lt;li&gt;Use a method group conversion to an instance (non-generic) method on a generic type, via a generic cache class with a single field in referring to an instance of the generic class &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;(Yes, the last one is a bit convoluted - but the line in the method itself is simple: &lt;font face="Courier New"&gt;&lt;strong&gt;Action foo = ClassHolder&amp;lt;T&amp;gt;.SampleInstance.NoOpInstance;&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Remember, we&amp;#39;re doing &lt;em&gt;each&lt;/em&gt; of these in a generic method, and calling that generic method using a type argument of either int or string. (I&amp;#39;ve run a few tests, and the exact type isn&amp;#39;t important - all that matters is that int is a value type, and string is a reference type.)&lt;/p&gt;  &lt;p&gt;Importantly, we&amp;#39;re &lt;em&gt;not&lt;/em&gt; capturing any variables, and the type parameter is &lt;em&gt;not&lt;/em&gt; involved in either the delegate type or any part of the implementation body.&lt;/p&gt;  &lt;h3&gt;Benchmark results&lt;/h3&gt;  &lt;p&gt;Again, times are in milliseconds - but this time I didn&amp;#39;t want to run it for 100 million iterations, as the &amp;quot;slow&amp;quot; versions would have taken far too long. I&amp;#39;ve run this on the x64 JIT as well and seen the same effect, but I haven&amp;#39;t included the figures here.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Times in milliseconds for 10 million iterations&lt;/strong&gt;&lt;/p&gt;  &lt;table border="1" cellspacing="0" cellpadding="2"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;&lt;strong&gt;Test&lt;/strong&gt;&lt;/td&gt;        &lt;td align="right"&gt;&lt;strong&gt;TestCase&amp;lt;int&amp;gt;&lt;/strong&gt;&lt;/td&gt;        &lt;td align="right"&gt;&lt;strong&gt;TestCase&amp;lt;string&amp;gt;&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;Lambda expression&lt;/td&gt;        &lt;td align="right"&gt;180&lt;/td&gt;        &lt;td align="right"&gt;29684&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;Generic cache class&lt;/td&gt;        &lt;td align="right"&gt;90&lt;/td&gt;        &lt;td align="right"&gt;288&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;Generic method group conversion&lt;/td&gt;        &lt;td align="right"&gt;184&lt;/td&gt;        &lt;td align="right"&gt;30017&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;Non-generic method group conversion&lt;/td&gt;        &lt;td align="right"&gt;178&lt;/td&gt;        &lt;td align="right"&gt;189&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;Static method on generic type&lt;/td&gt;        &lt;td align="right"&gt;180&lt;/td&gt;        &lt;td align="right"&gt;29276&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;Instance method on generic type&lt;/td&gt;        &lt;td align="right"&gt;202&lt;/td&gt;        &lt;td align="right"&gt;299&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;Yes, it&amp;#39;s about &lt;em&gt;150 times&lt;/em&gt; slower to create a delegate from a generic method with a reference type as the type argument than with a value type... and yet this is the first I&amp;#39;ve heard of this. (I wouldn&amp;#39;t be surprised if there were a post from the CLR team about it somewhere, but I don&amp;#39;t think it&amp;#39;s common knowledge by any means.)&lt;/p&gt;  &lt;h3&gt;Conclusion&lt;/h3&gt;  &lt;p&gt;One of the tricky things is that it&amp;#39;s hard to know exactly what the C# compiler is going to do with any given lambda expression. In fact, the method which was causing me grief earlier on &lt;em&gt;isn&amp;#39;t&lt;/em&gt; generic, but it&amp;#39;s in a generic type and captures some variables which use the type parameters - so perhaps that&amp;#39;s causing a generic method group conversion somewhere along the way.&lt;/p&gt;  &lt;p&gt;Noda Time is a relatively extreme case, but if you&amp;#39;re using delegates in any performance-critical spots, you should really be aware of this issue. I&amp;#39;m going to ping Microsoft (first informally, and then via a Connect report if that would be deemed useful) to see if there&amp;#39;s an awareness of this internally as potential &amp;quot;gotcha&amp;quot;, and whether there&amp;#39;s anything that can be done. Normal trade-offs of work required vs benefit apply, of course. It&amp;#39;s possible that this really is an edge case... but with lambdas flying everywhere these days, I&amp;#39;m not sure that it is.&lt;/p&gt;  &lt;p&gt;Maybe tomorrow I&amp;#39;ll actually be able to finish getting Noda Time moved onto the new system... all of this performance work has been a fun if surprising distraction from the main job of shipping working code...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1798036" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/cXsL5tTiNHw" height="1" width="1"/&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Benchmarking/default.aspx">Benchmarking</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Noda+Time/default.aspx">Noda Time</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2011/08/23/optimization-and-generics-part-2-lambda-expressions-and-reference-types.aspx</feedburner:origLink></item><item><title>Optimization and generics, part 1: the new() constraint (updated: now with CLR v2 results)</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/9OK-4m3wBiU/optimization-and-generics-part-1-the-new-constraint.aspx</link><pubDate>Mon, 22 Aug 2011 21:57:55 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1798032</guid><dc:creator>skeet</dc:creator><slash:comments>14</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1798032</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1798032</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2011/08/22/optimization-and-generics-part-1-the-new-constraint.aspx#comments</comments><description>&lt;p&gt;&lt;strong&gt;As with almost any performance work, your mileage may vary (in particular the 64-bit JIT may work differently) and you almost certainly shouldn&amp;#39;t care. Relatively few people write production code which is worth micro-optimizing. Please don&amp;#39;t take this post as an invitation to make code more complicated for the sake of irrelevant and possibly mythical performance changes.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;I&amp;#39;ve been doing quite a bit of work on Noda Time recently - and have started getting my head round all the work that James Keesey has put into the parsing/formatting. I&amp;#39;ve been reworking it so that we can do everything without throwing any exceptions, and also to work on the idea of parsing a pattern once and building a sequence of actions for both formatting and parsing from the action. To format or parse a value, we then just need to apply the actions in turn. &lt;a href="http://www.comparethemeerkat.com/"&gt;Simples&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Given that this is all in the name of performance (and &lt;a href="http://noda-time.blogspot.com/2010/02/performance-matters.html"&gt;I consider Noda Time to be worth optimizing pretty hard&lt;/a&gt;) I was pretty cross when I ran a complete revamp through the little benchmarking tool we use, and found that my rework had made everything &lt;em&gt;much&lt;/em&gt; slower. Even parsing a value &lt;em&gt;after parsing the pattern&lt;/em&gt; was slower than parsing both the value and the pattern together. Something was clearly very wrong.&lt;/p&gt;  &lt;p&gt;In fact, it turns out that at least two things were very wrong. The first (the subject of this post) was easy to fix and actually made the code a little more flexible. The second (the subject of the next post, which may be tomorrow) is going to be harder to work around.&lt;/p&gt;  &lt;h3&gt;The new() constraint&lt;/h3&gt;  &lt;p&gt;In my SteppedPattern type, I have a generic type parameter - TBucket. It&amp;#39;s already constrained in terms of another type parameter, but that&amp;#39;s irrelevant as far as I&amp;#39;m aware. (After today though, I&amp;#39;m taking very little for granted...) The important thing is that before I try to parse a &lt;em&gt;value&lt;/em&gt;, I want to create a new bucket. The idea is that bits of information end up in the bucket as they&amp;#39;re being parsed, and at the very end we put everything together. So each parse operation requires a new bucket. How can we create one in a nice generic way?&lt;/p&gt;  &lt;p&gt;Well, we can just call its public parameterless constructor. I don&amp;#39;t mind the types involved having such a constructor, so all we need to do is add the new() constraint, and then we can call new TBucket():&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="InlineComment"&gt;// Somewhat simplified...&lt;/span&gt;     &lt;br /&gt;&lt;span class="Modifier"&gt;internal&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;sealed&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; SteppedPattern&amp;lt;TResult, TBucket&amp;gt; : IParsePattern&amp;lt;TResult&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Linq"&gt;where&lt;/span&gt; TBucket : &lt;span class="Keyword"&gt;new&lt;/span&gt;()     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt; ParseResult&amp;lt;TResult&amp;gt; Parse(&lt;span class="ReferenceType"&gt;string&lt;/span&gt; value)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; TBucket bucket = &lt;span class="Keyword"&gt;new&lt;/span&gt; TBucket();     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Rest of parsing goes here&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;Great! Nice and simple. Unfortunately, it turned out that that one line of code was taking 75% of the time to parse a value. Just creating an empty bucket - pretty much the simplest bit of parsing. I was amazed when I discovered that.&lt;/p&gt;  &lt;h3&gt;Fixing it with a provider&lt;/h3&gt;  &lt;p&gt;The fix is reasonably easy. We just need to tell the type how to create an instance, and we can do that with a delegate:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="InlineComment"&gt;// Somewhat simplified...&lt;/span&gt;     &lt;br /&gt;&lt;span class="Modifier"&gt;internal&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;sealed&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; SteppedPattern&amp;lt;TResult, TBucket&amp;gt; : IParsePattern&amp;lt;TResult&amp;gt;     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; Func&amp;lt;TBucket&amp;gt; bucketProvider;     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;internal&lt;/span&gt; SteppedPattern(Func&amp;lt;TBucket&amp;gt; bucketProvider)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Keyword"&gt;this&lt;/span&gt;.bucketProvider = bucketProvider;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt; ParseResult&amp;lt;TResult&amp;gt; Parse(&lt;span class="ReferenceType"&gt;string&lt;/span&gt; value)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; TBucket bucket = bucketProvider();     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Rest of parsing goes here&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;Now I can just call new SteppedPattern(() =&amp;gt; new OffsetBucket()) or whatever. This also means I can keep the constructor internal, not that I care much. I could even reuse old parse buckets if that wouldn&amp;#39;t be a semantic problem - in other cases it could be useful. Hooray for lambda expressions - until we get to the next post, anyway.&lt;/p&gt;  &lt;h3&gt;Show me the figures!&lt;/h3&gt;  &lt;p&gt;You don&amp;#39;t want to have to run Noda Time&amp;#39;s benchmarks to see the results for yourself, so I wrote a small benchmark to time &lt;em&gt;just&lt;/em&gt; the construction of a generic type. As a measure of how insignificant this would be for most apps, these figures are in milliseconds, performing 100 &lt;em&gt;million&lt;/em&gt; iterations of the action in question. Unless you&amp;#39;re going to do this in performance-critical code, you just shouldn&amp;#39;t care.&lt;/p&gt;  &lt;p&gt;Anyway, the benchmark has four custom types: two classes, and two structs - a small and a large version of each. The small version has a single int field; the large version has eight long fields. For each type, I benchmarked both approaches to initialization.&lt;/p&gt;  &lt;p&gt;The results on two machines (32-bit and 64-bit) are below, for both the v2 CLR and v4. The 64-bit machine is much faster in general - you should only compare results within one machine, as it were.)&lt;/p&gt;  &lt;h3&gt;&lt;strong&gt;CLR v4: 32-bit results (ms per 100 million iterations)&lt;/strong&gt;&lt;/h3&gt;  &lt;table border="1" cellspacing="0" cellpadding="2" width="400"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Test type&lt;/td&gt;        &lt;td valign="top" width="133"&gt;new() constraint&lt;/td&gt;        &lt;td valign="top" width="133"&gt;Provider delegate&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Small struct&lt;/td&gt;        &lt;td valign="top" width="133"&gt;689&lt;/td&gt;        &lt;td valign="top" width="133"&gt;1225&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Large struct&lt;/td&gt;        &lt;td valign="top" width="133"&gt;11188&lt;/td&gt;        &lt;td valign="top" width="133"&gt;7273&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Small class&lt;/td&gt;        &lt;td valign="top" width="133"&gt;16307&lt;/td&gt;        &lt;td valign="top" width="133"&gt;1690&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Large class&lt;/td&gt;        &lt;td valign="top" width="133"&gt;17471&lt;/td&gt;        &lt;td valign="top" width="133"&gt;3017&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;&lt;strong&gt;CLR v4: 64-bit results (ms per 100 million iterations)&lt;/strong&gt;&lt;/p&gt;  &lt;table border="1" cellspacing="0" cellpadding="2" width="401"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Test type&lt;/td&gt;        &lt;td valign="top" width="133"&gt;new() constraint&lt;/td&gt;        &lt;td valign="top" width="133"&gt;Provider delegate&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Small struct&lt;/td&gt;        &lt;td valign="top" width="133"&gt;473&lt;/td&gt;        &lt;td valign="top" width="133"&gt;868&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Large struct&lt;/td&gt;        &lt;td valign="top" width="133"&gt;2670&lt;/td&gt;        &lt;td valign="top" width="133"&gt;2396&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Small class&lt;/td&gt;        &lt;td valign="top" width="133"&gt;8366&lt;/td&gt;        &lt;td valign="top" width="133"&gt;1189&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Large class&lt;/td&gt;        &lt;td valign="top" width="133"&gt;8805&lt;/td&gt;        &lt;td valign="top" width="133"&gt;1529&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;h3&gt;&lt;strong&gt;CLR v2: 32-bit results (ms per 100 million iterations)&lt;/strong&gt;&lt;/h3&gt;  &lt;table border="1" cellspacing="0" cellpadding="2" width="400"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Test type&lt;/td&gt;        &lt;td valign="top" width="133"&gt;new() constraint&lt;/td&gt;        &lt;td valign="top" width="133"&gt;Provider delegate&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Small struct&lt;/td&gt;        &lt;td valign="top" width="133"&gt;703&lt;/td&gt;        &lt;td valign="top" width="133"&gt;1246&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Large struct&lt;/td&gt;        &lt;td valign="top" width="133"&gt;11411&lt;/td&gt;        &lt;td valign="top" width="133"&gt;7392&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Small class&lt;/td&gt;        &lt;td valign="top" width="133"&gt;143967&lt;/td&gt;        &lt;td valign="top" width="133"&gt;1791&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Large class&lt;/td&gt;        &lt;td valign="top" width="133"&gt;143107&lt;/td&gt;        &lt;td valign="top" width="133"&gt;2581&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;&lt;strong&gt;CLR v2: 64-bit results (ms per 100 million iterations)&lt;/strong&gt;&lt;/p&gt;  &lt;table border="1" cellspacing="0" cellpadding="2" width="401"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Test type&lt;/td&gt;        &lt;td valign="top" width="133"&gt;new() constraint&lt;/td&gt;        &lt;td valign="top" width="133"&gt;Provider delegate&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Small struct&lt;/td&gt;        &lt;td valign="top" width="133"&gt;510&lt;/td&gt;        &lt;td valign="top" width="133"&gt;686&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Large struct&lt;/td&gt;        &lt;td valign="top" width="133"&gt;2334&lt;/td&gt;        &lt;td valign="top" width="133"&gt;1731&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Small class&lt;/td&gt;        &lt;td valign="top" width="133"&gt;81801&lt;/td&gt;        &lt;td valign="top" width="133"&gt;1539&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Large class&lt;/td&gt;        &lt;td valign="top" width="133"&gt;83293&lt;/td&gt;        &lt;td valign="top" width="133"&gt;1896&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;(An earlier version of this post had a mistake - my original tests used structs for everything, despite the names.)&lt;/p&gt;  &lt;p&gt;Others have reported slightly different results, including the new() constraint being better for both large &lt;em&gt;and&lt;/em&gt; small structs.&lt;/p&gt;  &lt;p&gt;Just in case you hadn&amp;#39;t spotted them, look at the results for classes. Those are the real results - it took over 2 minutes to run the test using the new() constraint on my 32-bit laptop, compared with under two seconds for the provider. Yikes. This was actually the situation I was in for Noda Time, which is built on .NET 2.0 - it&amp;#39;s not surprising that so much of my benchmark&amp;#39;s time was spent constructing classes, given results like this.&lt;/p&gt;  &lt;p&gt;Of course you can &lt;a href="http://pobox.com/~skeet/csharp/blogfiles/NewConstraint.cs"&gt;download the benchmark program&lt;/a&gt; for yourself and see how it performs on your machine. It&amp;#39;s a pretty cheap-and-cheerful benchmark, but when the differences are this big, minor sources of inaccuracy don&amp;#39;t bother me too much. The simplest way to run under CLR v2 is to compile with the .NET 3.5 C# compiler to start with.&lt;/p&gt;  &lt;h3&gt;What&amp;#39;s going on under the hood?&lt;/h3&gt;  &lt;p&gt;As far as I&amp;#39;m aware, there&amp;#39;s no IL to give support for the new() constraint. Instead, the compiler emits a call to &lt;a href="http://msdn.microsoft.com/en-us/library/0hcyx2kd.aspx"&gt;Activator.CreateInstance&amp;lt;T&amp;gt;&lt;/a&gt;. Apparently, that&amp;#39;s slower than calling a delegate - presumably due to trying to find an accessible constructor with reflection, and invoking it. I suspect it could be optimized relatively easily - e.g. by caching the results per type it&amp;#39;s called with, in terms of delegates. I&amp;#39;m slightly surprised this &lt;em&gt;hasn&amp;#39;t&lt;/em&gt; (apparently) been optimized, given how easy it is to cache values by generic type. No doubt there&amp;#39;s a good reason lurking there somewhere, even if it&amp;#39;s only the memory taken up by the cache.&lt;/p&gt;  &lt;p&gt;Either way, it&amp;#39;s easy to work around in general.&lt;/p&gt;  &lt;h3&gt;Conclusion&lt;/h3&gt;  &lt;p&gt;I wouldn&amp;#39;t have found this gotcha if I didn&amp;#39;t have before and after tests (or in this case, side-by-side tests of the old way and the new way of parsing). The real lesson of this post shouldn&amp;#39;t be about the new() constraint - it should be how important it is to test performance (assuming you care), and how easy it is to assume certain operations are cheap.&lt;/p&gt;  &lt;p&gt;Next post: something much weirder.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1798032" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/9OK-4m3wBiU" height="1" width="1"/&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Benchmarking/default.aspx">Benchmarking</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Noda+Time/default.aspx">Noda Time</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2011/08/22/optimization-and-generics-part-1-the-new-constraint.aspx</feedburner:origLink></item><item><title>Speaking engagement: Progressive .NET, London, September 7th</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/DX0mBc8e-G0/speaking-engagement-progressive-net-london-september-7th.aspx</link><pubDate>Thu, 28 Jul 2011 20:29:40 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1796884</guid><dc:creator>skeet</dc:creator><slash:comments>11</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1796884</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1796884</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2011/07/28/speaking-engagement-progressive-net-london-september-7th.aspx#comments</comments><description>&lt;p&gt;Just a quick note to mention an event I&amp;#39;ll be speaking at in September. SkillsMatter will be hosting &lt;a href="http://skillsmatter.com/event/open-source-dot-net/progressive-dot-net-tutorials-2011/js-2399"&gt;Progressive .NET&lt;/a&gt;, a 3-day event set of tutorials on September 5th-7th in London. I&amp;#39;ll be speaking about C# 5&amp;#39;s async feature on the last day (9.30am-1pm) but there&amp;#39;s a host of other speakers too. Should be good. For my own part, with four hours or so to cover async, I should be able to cover both the high level stuff &lt;em&gt;and&lt;/em&gt; the implementation details, with plenty of time for the inevitable questions.&lt;/p&gt;  &lt;p&gt;This one isn&amp;#39;t free though, I&amp;#39;m afraid - it&amp;#39;s normally £425. Hardly pocket money, but pretty good value for three full days of deep-dive sessions. However, there are two bits of good news:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Readers of this blog can get £50 off using the promo code &amp;quot;PROGNET50&amp;quot; at the checkout. &lt;/li&gt;    &lt;li&gt;I have two free tickets to give away. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;In an effort to make the ticket give-away fair, I&amp;#39;m thinking of a 32-bit number - mail me (&lt;a href="mailto:skeet@pobox.com"&gt;skeet@pobox.com&lt;/a&gt;) an Int32, and the two readers with the closest value will get the tickets. Please include &amp;quot;Progressive .NET&amp;quot; in the subject line of the mail so I can filter them easily :)&lt;/p&gt;  &lt;p&gt;Anyway, hope to see you there - please grab me to say hi.&lt;/p&gt;  &lt;h3&gt;Update (August 4th): and the winners are...&lt;/h3&gt;  &lt;p&gt;Congratulations to &lt;strong&gt;The Configurator&lt;/strong&gt; and &lt;strong&gt;Haris Hasan&lt;/strong&gt; who submitted the closest numbers to the one I was thinking of: -890978631.&lt;/p&gt;  &lt;p&gt;In fact, The Configurator guessed the &lt;em&gt;exact&lt;/em&gt; value - which is the result of calling &amp;quot;Progressive .NET&amp;quot;.GetHashCode() on my 32-bit laptop running .NET 4. (I can&amp;#39;t remember which versions have different hash algorithms, but as it&amp;#39;s pretty arbitrary, it seemed good enough...) I&amp;#39;m impressed!&lt;/p&gt;  &lt;p&gt;I&amp;#39;ll be emailing SkillsMatter to let them know about the winners - and thanks to everyone else who mailed me a guess. Hope I&amp;#39;ll see some of you there anyway!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1796884" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/DX0mBc8e-G0" height="1" width="1"/&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Speaking+engagements/default.aspx">Speaking engagements</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+5/default.aspx">C# 5</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/async/default.aspx">async</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2011/07/28/speaking-engagement-progressive-net-london-september-7th.aspx</feedburner:origLink></item><item><title>Eduasync part 14: Data passing in coroutines</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/H7aDBFDyQRY/eduasync-part-14-data-passing-in-coroutines.aspx</link><pubDate>Wed, 29 Jun 2011 17:18:20 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1795430</guid><dc:creator>skeet</dc:creator><slash:comments>2</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1795430</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1795430</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2011/06/29/eduasync-part-14-data-passing-in-coroutines.aspx#comments</comments><description>&lt;p&gt;(This post covers project 19 in the &lt;a href="http://code.google.com/p/eduasync/source/browse/"&gt;source code&lt;/a&gt;.)&lt;/p&gt;  &lt;p&gt;Last time we looked at independent coroutines running in a round-robin fashion. This time we&amp;#39;ll keep the round-robin scheduling, but add in the idea of passing data from one coroutine to another. Each coroutine will act on data of the same type, which is necessary for the scheme to work when one coroutine could &amp;quot;drop out&amp;quot; of the chain by returning.&lt;/p&gt;  &lt;h3&gt;Designing the data flow&lt;/h3&gt;  &lt;p&gt;It took me a while to get to the stage where I was happy with the design of how data flowed around these coroutines. I knew I wanted a coordinator as before, and that it should have a Yield method taking the value to pass to the next coroutine and returning an awaitable which would provide the next value when it completed. The tricky part was working out what to do at the start of each method and the end. If the method just took a Coordinator parameter, we wouldn&amp;#39;t have anything to do with the value yielded by the first coroutine, because the second coroutine wouldn&amp;#39;t be ready to accept it yet. Likewise when a coroutine completed, we wouldn&amp;#39;t have another value to pass to the next coroutine.&lt;/p&gt;  &lt;p&gt;Writing these dilemmas out in this post, the solution seems blindingly obvious of course: each coroutine should accept a data value on entry, and return one at the end. At any point where we transfer control, we &lt;em&gt;provide&lt;/em&gt; a value and have a value which is &lt;em&gt;required&lt;/em&gt; by something. The final twist is to make the coordinator&amp;#39;s Start method take an initial value and return the value returned by the last coroutine to complete.&lt;/p&gt;  &lt;p&gt;So, that&amp;#39;s the theory... let&amp;#39;s look at the implementation.&lt;/p&gt;  &lt;h3&gt;Initialization&lt;/h3&gt;  &lt;p&gt;I&amp;#39;ve changed the coordinator to take all the coroutines as a constructor parameter (of the somewhat fearsome declaration &amp;quot;params Func&amp;lt;Coordinator&amp;lt;T&amp;gt;, T, Task&amp;lt;T&amp;gt;&amp;gt;[] coroutines&amp;quot;) which means we don&amp;#39;t need to implement IEnumerable pointlessly any more.&lt;/p&gt;  &lt;p&gt;This leads to a code skeleton of this form:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; Main(&lt;span class="ReferenceType"&gt;string&lt;/span&gt;[] args)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Linq"&gt;var&lt;/span&gt; coordinator = &lt;span class="Keyword"&gt;new&lt;/span&gt; Coordinator&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt;(FirstCoroutine,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SecondCoroutine,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ThirdCoroutine);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; finalResult = coordinator.Start(&lt;span class="String"&gt;&amp;quot;m1&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Final result: {0}&amp;quot;&lt;/span&gt;, finalResult);     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;async&lt;/span&gt; Task&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt; FirstCoroutine(     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Coordinator&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt; coordinator,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; initialValue)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ...     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;&lt;span class="InlineComment"&gt;// Same signature for SecondCoroutine and ThirdCoroutine&lt;/span&gt; &lt;/div&gt;  &lt;p&gt;Last time we simply had a Queue&amp;lt;Action&amp;gt; internally in the coordinator as the actions to invoke. You might be &lt;em&gt;expecting&lt;/em&gt; a Queue&amp;lt;Func&amp;lt;T, T&amp;gt;&amp;gt; this time - after all, we&amp;#39;re passing in data and returning data at each point. However, the mechanism for that data transfer is &amp;quot;out of band&amp;quot; so to speak. The only time we really &amp;quot;return&amp;quot; an item is when we reach the end of a coroutine. Usually we&amp;#39;ll be providing data to the next step using a method. Likewise the only time a coroutine is &lt;em&gt;given&lt;/em&gt; data directly is in the first call - after that, it will have to &lt;em&gt;fetch&lt;/em&gt; the value by calling GetResult() on the awaiter which it uses to yield control.&lt;/p&gt;  &lt;p&gt;All of this is leading to a requirement for our constructor to convert each coroutine delegate into a simple Action. The trick is working out how to deal with the data flow. I&amp;#39;m going to include SupplyValue() and ConsumeValue() methods within the coordinator for the awaiter to use, so it&amp;#39;s just a case of calling those appropriately from our action. In particular:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;When the action is called, it should consume the current value. &lt;/li&gt;    &lt;li&gt;It should then call the coroutine passing in the coordinator (&amp;quot;this&amp;quot;) and the initial value. &lt;/li&gt;    &lt;li&gt;When the task returned by the coroutine has completed, the result of that task should be used to supply a new value. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The only tricky part here is the last bullet - and it&amp;#39;s not that hard really, so long as we remember that we&amp;#39;re absolutely &lt;em&gt;not&lt;/em&gt; trying to start any new threads. We just want to hook onto the end of the task, getting a chance to supply the value before the next coroutine tries to pick it up. We can do that using Task.ContinueWith, but passing in TaskContinuationOptions.ExecuteSynchronously so that we use the same thread that the task completes on to execute the continuation.&lt;/p&gt;  &lt;p&gt;At this point we can implement the initialization part of the coordinator, assuming the presence of SupplyValue() and ConsumeValue():&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;sealed&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Coordinator&amp;lt;T&amp;gt;     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; Queue&amp;lt;Action&amp;gt; actions;     &lt;br /&gt;&lt;span class="Modifier"&gt;&amp;#160;&amp;#160;&amp;#160; private&lt;/span&gt; &lt;span class="Modifier"&gt;readonly&lt;/span&gt; Awaitable awaitable;     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt; Coordinator(&lt;span class="MethodParameter"&gt;params&lt;/span&gt; Func&amp;lt;Coordinator&amp;lt;T&amp;gt;, T, Task&amp;lt;T&amp;gt;&amp;gt;[] coroutines)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// We can&amp;#39;t refer to &amp;quot;this&amp;quot; in the variable initializer. We can use&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// the same awaitable for all yield calls.&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Keyword"&gt;this&lt;/span&gt;.awaitable = &lt;span class="Keyword"&gt;new&lt;/span&gt; Awaitable(&lt;span class="Keyword"&gt;this&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; actions = &lt;span class="Keyword"&gt;new&lt;/span&gt; Queue&amp;lt;Action&amp;gt;(coroutines.Select(ConvertCoroutine));     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Converts a coroutine into an action which consumes the current value,&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// calls the coroutine, and attaches a continuation to it so that the return&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// value is used as the new value.&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt; Action ConvertCoroutine(Func&amp;lt;Coordinator&amp;lt;T&amp;gt;, T, Task&amp;lt;T&amp;gt;&amp;gt; coroutine)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; () =&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Task&amp;lt;T&amp;gt; task = coroutine(&lt;span class="Keyword"&gt;this&lt;/span&gt;, ConsumeValue());     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; task.ContinueWith(ignored =&amp;gt; SupplyValue(task.Result),     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; TaskContinuationOptions.ExecuteSynchronously);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; };     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;I&amp;#39;ve broken ConvertCoroutine into a separate method so that we can use it as the projection for the Select call within the constructor. I did &lt;em&gt;initially&lt;/em&gt; have it within a lambda expression within the constructor, but it was utterly hideous in terms of readabililty.&lt;/p&gt;  &lt;p&gt;One suggestion I&amp;#39;ve received is that I could declare a new delegate type instead of using Func&amp;lt;Coordinator&amp;lt;T&amp;gt;, T, Task&amp;lt;T&amp;gt;&amp;gt; to represent a coroutine. This could either be a non-generic delegate nested in the generic coordinator class, or a generic stand-alone delegate:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;delegate&lt;/span&gt; T Coroutine&amp;lt;T&amp;gt;(Coordinator&amp;lt;T&amp;gt; coordinator, T initialValue);     &lt;br /&gt;    &lt;br /&gt;&lt;span class="InlineComment"&gt;// Or nested...&lt;/span&gt;     &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;sealed&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Coordinator&amp;lt;T&amp;gt;     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;delegate&lt;/span&gt; T Coroutine(Coordinator&amp;lt;T&amp;gt; coordinator, T initialValue);     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;Both of these would work perfectly well. I haven&amp;#39;t made the change at the moment, but it&amp;#39;s certainly worth considering. The debate about whether to use custom delegate types or Func/Action is one for another blog post, I think :)&lt;/p&gt;  &lt;p&gt;The one bit of the initialization I haven&amp;#39;t explained yet is the &amp;quot;awaitable&amp;quot; field and the Awaitable type. They&amp;#39;re to do with yielding - so let&amp;#39;s look at them now.&lt;/p&gt;  &lt;h3&gt;Yielding and transferring data&lt;/h3&gt;  &lt;p&gt;Next we need to work out how we&amp;#39;re going to transfer data and control between the coroutines. As I&amp;#39;ve mentioned, we&amp;#39;re going to use a method within the coordinator, called from the coroutines, to accomplish this. The coroutines have this sort of code:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;async&lt;/span&gt; Task&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt; FirstCoroutine(     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Coordinator&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt; coordinator,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; initialValue)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Starting FirstCoroutine with initial value {0}&amp;quot;&lt;/span&gt;,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; initialValue);&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ...     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ReferenceType"&gt;string&lt;/span&gt; received = &lt;span class="Modifier"&gt;await&lt;/span&gt; coordinator.Yield(&lt;span class="String"&gt;&amp;quot;x1&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Returned to FirstCoroutine with value {0}&amp;quot;&lt;/span&gt;, received);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ...     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;#160;&lt;span class="String"&gt;&amp;quot;x3&amp;quot;&lt;/span&gt;;     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;The method name &amp;quot;Yield&amp;quot; here is a double-edged sword. The word has two meanings - yielding a value to be used elsewhere, and yielding control until we&amp;#39;re called back. Normally it&amp;#39;s not ideal to use a name that can mean subtly different things - but in this case we actually &lt;em&gt;want&lt;/em&gt; both of these meanings.&lt;/p&gt;  &lt;p&gt;So, what does Yield need to do? Well, the flow control should look something like this:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Coroutine calls Yield() &lt;/li&gt;    &lt;li&gt;Yield() calls SupplyValue() internally to remember the new value to be consumed by the next coroutine &lt;/li&gt;    &lt;li&gt;Yield() returns an awaitable to the coroutine &lt;/li&gt;    &lt;li&gt;Due to the await expression, the coroutine calls GetAwaiter() on the awaitable to get an awaiter &lt;/li&gt;    &lt;li&gt;The coroutine checks IsCompleted on the awaiter, which must return false (to prompt the remaining behaviour) &lt;/li&gt;    &lt;li&gt;The coroutine calls OnCompleted() passing in the continuation for the rest of the method &lt;/li&gt;    &lt;li&gt;The coroutine returns to its caller &lt;/li&gt;    &lt;li&gt;The coordinator proceeds with the next coroutine &lt;/li&gt;    &lt;li&gt;When we eventually get back to this coroutine, it will call GetResult() to get the &amp;quot;current value&amp;quot; to assign to the &amp;quot;received&amp;quot; variable. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Now you&amp;#39;ll see that Yield() needs to return some kind of awaitable type - in other words, one with a GetAwaiter() method. Previously we put this directly on the Coordinator type, and we &lt;em&gt;could&lt;/em&gt; have done that here - but I don&amp;#39;t really want anyone to just &amp;quot;await coordinator&amp;quot; accidentally. You should really need to call Yield() in order to get an awaitable. So we have an Awaitable type, nested in Coordinator.&lt;/p&gt;  &lt;p&gt;We then need to decide what the &lt;em&gt;awaiter&lt;/em&gt; type is - the result of calling GetAwaiter() on the awaitable. This time I decided to use the Coordinator itself. That means people &lt;em&gt;could&lt;/em&gt; accidentally call IsCompleted, OnCompleted() or GetResult(), but I figured that wasn&amp;#39;t too bad. If we were to go to the extreme, we&amp;#39;d create another type just for the Awaiter as well. It would need to have a reference to the coordinator of course, in order to actually do its job. As it is, we can make the Awaitable just return the Coordinator that created it. (Awaitable is nested within Coordinator&amp;lt;T&amp;gt;, which is how it can refer to T without being generic itself.)&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;sealed&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Awaitable     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; Coordinator&amp;lt;T&amp;gt; coordinator;     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;internal&lt;/span&gt; Awaitable(Coordinator&amp;lt;T&amp;gt; coordinator)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Keyword"&gt;this&lt;/span&gt;.coordinator = coordinator;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt; Coordinator&amp;lt;T&amp;gt; GetAwaiter()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; coordinator;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;The only state here is the coordinator, which is why we create an instance of Awaitable on the construction of the Coordinator, and keep it around.&lt;/p&gt;  &lt;p&gt;Now Yield() is really simple:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt; Awaitable Yield(T value)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; SupplyValue(value);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; awaitable;     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;So to recap, we now just need the awaiter members, SupplyValue() and ConsumeValue(). Let&amp;#39;s look at the awaiter members (in Coordinator) to start with. We already know that IsCompleted will just return false. OnCompleted() just needs to stash the continuation in the queue, and GetResult() just needs to consume the &amp;quot;current&amp;quot; value and return it:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;bool&lt;/span&gt; IsCompleted { get { &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;false&lt;/span&gt;; } }     &lt;br /&gt;    &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; OnCompleted(Action continuation)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; actions.Enqueue(continuation);     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt; T GetResult()     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; ConsumeValue();     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;Simple, huh? Finally, consuming and supplying values:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt; T currentValue;     &lt;br /&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;bool&lt;/span&gt; valuePresent;     &lt;br /&gt;    &lt;br /&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; SupplyValue(T value)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;if&lt;/span&gt; (valuePresent)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;throw&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; InvalidOperationException     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (&lt;span class="String"&gt;&amp;quot;Attempt to supply value when one is already present&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; currentValue = value;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; valuePresent = &lt;span class="Keyword"&gt;true&lt;/span&gt;;     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt; T ConsumeValue()     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;if&lt;/span&gt; (!valuePresent)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;throw&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; InvalidOperationException     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (&lt;span class="String"&gt;&amp;quot;Attempt to consume value when it isn&amp;#39;t present&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; T oldValue = currentValue;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; valuePresent = &lt;span class="Keyword"&gt;false&lt;/span&gt;;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; currentValue = &lt;span class="Modifier"&gt;default&lt;/span&gt;(T);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; oldValue;     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;These are relatively long methods (compared with the other ones I&amp;#39;ve shown) but pretty simple. Hopefully they don&amp;#39;t need explanation :)&lt;/p&gt;  &lt;h3&gt;The results&lt;/h3&gt;  &lt;p&gt;Now that everything&amp;#39;s in place, we can run it. I haven&amp;#39;t posted the full code of the coroutines, but you can &lt;a href="http://code.google.com/p/eduasync/source/browse/src/HomogeneousCoroutines/Program.cs"&gt;see it on Google Code&lt;/a&gt;. Hopefully the results speak for themselves though - you can see the relevant values passing from one coroutine to another (and in and out of the Start method).&lt;/p&gt;  &lt;div class="code"&gt;Starting FirstCoroutine with initial value m1   &lt;br /&gt;Yielding &amp;#39;x1&amp;#39; from FirstCoroutine...    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Starting SecondCoroutine with initial value x1    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Starting SecondCoroutine    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Yielding &amp;#39;y1&amp;#39; from SecondCoroutine...    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Starting ThirdCoroutine with initial value y1    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Yielding &amp;#39;z1&amp;#39; from ThirdCoroutine...    &lt;br /&gt;Returned to FirstCoroutine with value z1    &lt;br /&gt;Yielding &amp;#39;x2&amp;#39; from FirstCoroutine...    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Returned to SecondCoroutine with value x2    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Yielding &amp;#39;y2&amp;#39; from SecondCoroutine...    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Returned to ThirdCoroutine with value y2    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Finished ThirdCoroutine...    &lt;br /&gt;Returned to FirstCoroutine with value z2    &lt;br /&gt;Finished FirstCoroutine    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Returned to SecondCoroutine with value x3    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Yielding &amp;#39;y3&amp;#39; from SecondCoroutine...    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Returned to SecondCoroutine with value y3    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Finished SecondCoroutine    &lt;br /&gt;Final result: y4 &lt;/div&gt;  &lt;h3&gt;Conclusion&lt;/h3&gt;  &lt;p&gt;I&amp;#39;m not going to claim this is the world&amp;#39;s most useful coroutine model - or indeed useful at all. As ever, I&amp;#39;m more interested in thinking about how data and control flow can be modelled than actual usefulness.&lt;/p&gt;  &lt;p&gt;In this case, it was the realization that &lt;em&gt;everything&lt;/em&gt; should accept and return a value of the same type which really made it all work. After that, the actual code is pretty straightforward. (At least, I think it is - please let me know if any bits are confusing, and I&amp;#39;ll try to elaborate on them.)&lt;/p&gt;  &lt;p&gt;Next time we&amp;#39;ll look at something more like a pipeline model - something remarkably reminiscent of LINQ, but without taking up as much stack space (and with vastly worse readability, of course). Unfortunately the current code reaches the limits of my ability to understand why it works, which means it far exceeds my ability to &lt;em&gt;explain&lt;/em&gt; why it works. Hopefully I can simplify it a bit over the next few days.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1795430" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/H7aDBFDyQRY" height="1" width="1"/&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+5/default.aspx">C# 5</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/async/default.aspx">async</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Eduasync/default.aspx">Eduasync</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2011/06/29/eduasync-part-14-data-passing-in-coroutines.aspx</feedburner:origLink></item><item><title>Eduasync part 13: first look at coroutines with async</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/TRwK6BPlGqE/eduasync-part-13-first-look-at-coroutines-with-async.aspx</link><pubDate>Wed, 22 Jun 2011 20:37:30 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1795089</guid><dc:creator>skeet</dc:creator><slash:comments>14</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1795089</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1795089</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2011/06/22/eduasync-part-13-first-look-at-coroutines-with-async.aspx#comments</comments><description>&lt;p&gt;(This part covers project 18 in the &lt;a href="http://code.google.com/p/eduasync/source/browse/"&gt;source code&lt;/a&gt;.)&lt;/p&gt;  &lt;p&gt;As I mentioned in earlier parts, the &amp;quot;awaiting&amp;quot; part of async methods is in no way limited to tasks. So long as we have a suitable GetAwaiter() method which returns a value of a type which in turn has suitable methods on it, the compiler doesn&amp;#39;t really care what&amp;#39;s going on. It&amp;#39;s time to exploit that to implement some form of &lt;a href="http://en.wikipedia.org/wiki/Coroutine"&gt;coroutines&lt;/a&gt; in C#.&lt;/p&gt;  &lt;h3&gt;Introduction to coroutines&lt;/h3&gt;  &lt;p&gt;The fundamental idea of coroutines is to have multiple methods executing cooperatively, each of them maintaining their position within the coroutine when they yield to another. You can &lt;em&gt;almost&lt;/em&gt; think of them as executing in multiple threads, with only one thread actually &lt;em&gt;running&lt;/em&gt; at a time, and signalling between the different threads to control flow. However, we don&amp;#39;t really need multiple threads once we&amp;#39;ve got continuations - we can have a single thread with a complex flow of continuations, and still only a very short &amp;quot;real&amp;quot; stack. (The control flow is stored in normal collections instead of being implicit on the thread&amp;#39;s stack.)&lt;/p&gt;  &lt;p&gt;Coroutines were already feasible in C# through the use of iterator blocks, but the async feature of C# allows a slightly more natural way of expressing them, in my view. (The linked Wikipedia page gives a sketch of how coroutines can be built on top of &lt;em&gt;generators&lt;/em&gt;, which in the general concept that iterator blocks implement in C#.)&lt;/p&gt;  &lt;p&gt;I have implemented various flavours of coroutines in Eduasync. It&amp;#39;s possible that some (all?) of them shouldn&amp;#39;t &lt;em&gt;strictly &lt;/em&gt;be called coroutines, but they&amp;#39;re close enough to the real thing in feeling. This is far from an exhaustive set of approaches. Once you&amp;#39;ve got the basic idea of what I&amp;#39;m doing, you may well want to experiment with your own implementations.&lt;/p&gt;  &lt;p&gt;I&amp;#39;m &lt;em&gt;not&lt;/em&gt; going to claim that the use of coroutines in any of my examples really makes any sense in terms of making real tasks easier. This is &lt;em&gt;purely&lt;/em&gt; for the sake of interest and twisting the async feature for fun.&lt;/p&gt;  &lt;h3&gt;Round-robin independent coroutines&lt;/h3&gt;  &lt;p&gt;Our first implementation of coroutines is relatively simple. A &lt;em&gt;coordinator&lt;/em&gt; effectively &amp;quot;schedules&amp;quot; the coroutines it&amp;#39;s set up with in a round-robin fashion: when one of the coroutines yields control to the coordinator, the coordinator remembers where the coroutine had got to, and then starts the next one. When each coroutine has executed its first piece of code and yielded control, the coordinator will go back to the first coroutine to continue execution, and so on until all coroutines have completed.&lt;/p&gt;  &lt;p&gt;The coroutines don&amp;#39;t know about each other, and no data is being passed between them.&lt;/p&gt;  &lt;p&gt;Hopefully it&amp;#39;s reasonably obvious that the coordinator contains all the smarts here - the coroutines themselves can be relatively dumb. Let&amp;#39;s look at what the client code looks like (along with the results) before we get to the coordinator code.&lt;/p&gt;  &lt;h3&gt;Client code&lt;/h3&gt;  &lt;p&gt;The sample code contains three coroutines, all of which take a Coordinator parameter and have a void return type. These are passed to a new coordinator using a collection initializer and method group conversions; the coordinator is then started. Here&amp;#39;s the entry point code for this:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; Main(&lt;span class="ReferenceType"&gt;string&lt;/span&gt;[] args)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Linq"&gt;var&lt;/span&gt; coordinator = &lt;span class="Keyword"&gt;new&lt;/span&gt; Coordinator {&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FirstCoroutine,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SecondCoroutine,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ThirdCoroutine     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; };     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; coordinator.Start();     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;When each coroutine is initially started, the coordinator passes a reference to &lt;em&gt;itself&lt;/em&gt; as the argument to the coroutine. That&amp;#39;s how we solve the chicken-and-egg problem of the coroutine and coordinator having to know about each other. The way a coroutine yields control is simply by awaiting the coordinator. The result type of this await expression is void - it&amp;#39;s just a way of &amp;quot;pausing&amp;quot; the coroutine.&lt;/p&gt;  &lt;p&gt;We&amp;#39;re not doing anything interesting in the actual coroutines - just tracing the execution flow. Of course we &lt;em&gt;could&lt;/em&gt; do anything we wanted, within reason. We could even await a &lt;em&gt;genuinely&lt;/em&gt; asynchronous task such as fetching a web page asynchronously. In that case the whole coroutine collection would be &amp;quot;paused&amp;quot; until the fetch returned.&lt;/p&gt;  &lt;p&gt;Here&amp;#39;s the code for the first coroutine - the second and third ones are similar, but use different indentation for clarity. The third coroutine is also shorter, just for fun - it only awaits the coordinator once.&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;async&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; FirstCoroutine(Coordinator coordinator)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Starting FirstCoroutine&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Yielding from FirstCoroutine...&amp;quot;&lt;/span&gt;);     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;await&lt;/span&gt; coordinator;     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Returned to FirstCoroutine&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Yielding from FirstCoroutine again...&amp;quot;&lt;/span&gt;);     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;await&lt;/span&gt; coordinator;     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Returned to FirstCoroutine again&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Finished FirstCoroutine&amp;quot;&lt;/span&gt;);     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;And here&amp;#39;s the output...&lt;/p&gt;  &lt;div class="code"&gt;Starting FirstCoroutine    &lt;br /&gt;Yielding from FirstCoroutine...     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Starting SecondCoroutine     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Yielding from SecondCoroutine...     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Starting ThirdCoroutine     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Yielding from ThirdCoroutine...     &lt;br /&gt;Returned to FirstCoroutine     &lt;br /&gt;Yielding from FirstCoroutine again...     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Returned to SecondCoroutine     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Yielding from SecondCoroutine again...     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Returned to ThirdCoroutine     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Finished ThirdCoroutine...     &lt;br /&gt;Returned to FirstCoroutine again     &lt;br /&gt;Finished FirstCoroutine     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Returned to SecondCoroutine again     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Finished SecondCoroutine &lt;/div&gt;  &lt;p&gt;Hopefully that&amp;#39;s the output you expected, given the earlier description. Again it may help if you think of the coroutines as running in separate pseudo-threads: the execution within each pseudo-thread is just linear, and the timing is controlled by our explicit &amp;quot;await&amp;quot; expressions. All of this would actually be pretty easy to implement using multiple threads which really &lt;em&gt;did&lt;/em&gt; just block on each await expression - but the fun part is keeping it all in one real thread. Let&amp;#39;s have a look at the coordinator.&lt;/p&gt;  &lt;h3&gt;The Coordinator class&lt;/h3&gt;  &lt;p&gt;Some of the later coroutine examples end up being slightly brainbusting, at least for me. This one is relatively straightforward though, once you&amp;#39;ve got the basic idea. All we need is a &lt;em&gt;queue&lt;/em&gt; of actions to execute. After initialization, we want our queue to contain the coroutine starting points.&lt;/p&gt;  &lt;p&gt;When a coroutine yields control, we just need to add the remainder of it to the end of the queue, and move on to the next item. Obviously the async infrastructure will provide &amp;quot;the remainder of the coroutine&amp;quot; as a continuation via the OnContinue method.&lt;/p&gt;  &lt;p&gt;When a coroutine just returns, we continue with the next item in the queue as before - it&amp;#39;s just that we won&amp;#39;t add a continuation to the end of the queue. Eventually (well, hopefully) we&amp;#39;ll end up with an empty queue, at which point we can stop.&lt;/p&gt;  &lt;h3&gt;Initialization and a choice of data structures&lt;/h3&gt;  &lt;p&gt;We&amp;#39;ll represent our queue using Queue&amp;lt;T&amp;gt; where the T is a delegate type. We have two choices here though, because we have two kinds of delegate - one which takes the Coordinator as a parameter (for the initial coroutine setup) and one which has no parameters (for the continuations). Fortunately we can convert between the two in either direction very simply, bearing in mind that all of this is within the context of a coordinator. For example:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="InlineComment"&gt;// If we&amp;#39;re given a coroutine and want a plain Action &lt;/span&gt;    &lt;br /&gt;Action&amp;lt;Coordinator&amp;gt; coroutine = ...;&amp;#160; &lt;br /&gt;Action action = () =&amp;gt; coroutine(&lt;span class="Keyword"&gt;this&lt;/span&gt;);    &lt;br /&gt;    &lt;br /&gt;&lt;span class="InlineComment"&gt;// If we&amp;#39;re given a plain Action and want an Action&amp;lt;Continuation&amp;gt;: &lt;/span&gt;    &lt;br /&gt;Action continuation = ...;&amp;#160; &lt;br /&gt;Action&amp;lt;Coordinator&amp;gt; coroutine = ignored =&amp;gt; continuation(); &lt;/div&gt;  &lt;p&gt;I&amp;#39;ve arbitrarily chosen to use the first option, so there&amp;#39;s a Queue&amp;lt;Action&amp;gt; internally.&lt;/p&gt;  &lt;p&gt;Now we need to get the collection initializer working. The C# compiler requires an appropriate Add method (which is easy) and also checks that the type implements IEnumerable. We don&amp;#39;t really need to be able to iterate over the queue of actions, so I&amp;#39;ve use explicit interface implementation to reduce the likelihood of GetEnumerator() being called inappropriately, and made the method throw an exception for good measure. That gives us the skeleton of the class required for setting up:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;sealed&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Coordinator : IEnumerable    &lt;br /&gt;{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; Queue&amp;lt;Action&amp;gt; actions = &lt;span class="Keyword"&gt;new&lt;/span&gt; Queue&amp;lt;Action&amp;gt;();    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Used by collection initializer to specify the coroutines to run&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; Add(Action&amp;lt;Coordinator&amp;gt; coroutine)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; actions.Enqueue(() =&amp;gt; coroutine(&lt;span class="Keyword"&gt;this&lt;/span&gt;));    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Required for collection initializers, but we don&amp;#39;t really want&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// to expose anything.&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; IEnumerator IEnumerable.GetEnumerator()    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;throw&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; NotSupportedException(&lt;span class="String"&gt;&amp;quot;IEnumerable only supported to enable collection initializers&amp;quot;&lt;/span&gt;);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;(Note that I haven&amp;#39;t used XML documentation anywhere here - it&amp;#39;s great for real code, but adds clutter in blog posts.)&lt;/p&gt;  &lt;p&gt;For production code I&amp;#39;d probably prevent Add from being called after the coordinator had been started, but there&amp;#39;s no need to do it in our well-behaved sample code. We&amp;#39;re only going to add extra actions to the queue via continuations, which will be added due to await expressions.&lt;/p&gt;  &lt;h3&gt;The main execution loop and async infrastructure&lt;/h3&gt;  &lt;p&gt;So far we&amp;#39;ve got code to register coroutines in the queue - so now we need to execute them. Bearing in mind that the actions themselves will be responsible for adding continuations, the main loop of the coordinator is embarrassingly simple:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="InlineComment"&gt;// Execute actions in the queue until it&amp;#39;s empty. Actions add *more*&lt;/span&gt;    &lt;br /&gt;&lt;span class="InlineComment"&gt;// actions (continuations) to the queue by awaiting this coordinator.&lt;/span&gt;    &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; Start()    &lt;br /&gt;{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;while&lt;/span&gt; (actions.Count &amp;gt; 0)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; actions.Dequeue().Invoke();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;Of course, the interesting bit is the code which supports the async methods and await expressions. We know we need to provide a GetAwaiter() method, but what should that return? Well, we&amp;#39;re just going to use the awaiter to add a continuation to the coordinator&amp;#39;s queue. It&amp;#39;s got no other state than that - so we might as well return the coordinator itself, and put the other infrastructure methods directly in the coordinator.&lt;/p&gt;  &lt;p&gt;Again, this is &lt;em&gt;slightly&lt;/em&gt; ugly, as the extra methods don&amp;#39;t really make sense on the coordinator - we wouldn&amp;#39;t want to call them directly from client code, for example. However, they&amp;#39;re fairly irrelevant - we could always create a nested type which just had a reference to its &amp;quot;parent&amp;quot; coordinator if we wanted to. For simplicity, I haven&amp;#39;t bothered with this - I&amp;#39;ve just implemented GetAwaiter() trivially:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="InlineComment"&gt;// Used by await expressions to get an awaiter&lt;/span&gt;    &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt; Coordinator GetAwaiter()    &lt;br /&gt;{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;this&lt;/span&gt;;    &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;So, that leaves just three members still to implement: IsCompleted, OnCompleted and GetResult. We always want the IsCompleted property to return false, as otherwise the coroutine will just continue executing &lt;em&gt;immediately&lt;/em&gt; without returning to cede control; the await expression would be pointless. OnCompleted &lt;em&gt;just&lt;/em&gt; needs to add the continuation to the end of the queue - we don&amp;#39;t need to attach it to a task, or anything like that. Finally, GetResult is a no-op - we have no results, no exceptions, and basically nothing to do. You might want to add a bit of logging here, if you were so inclined, but there&amp;#39;s no real need.&lt;/p&gt;  &lt;p&gt;So, here are the final three members of Coordinator:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="InlineComment"&gt;// Force await to yield control&lt;/span&gt;    &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;bool&lt;/span&gt; IsCompleted { get { &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;false&lt;/span&gt;; } }    &lt;br /&gt;    &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; OnCompleted(Action continuation)    &lt;br /&gt;{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Put the continuation at the end of the queue, ready to&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// execute when the other coroutines have had a go.&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; actions.Enqueue(continuation);    &lt;br /&gt;}    &lt;br /&gt;    &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; GetResult()    &lt;br /&gt;{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Our await expressions are void, and we never need to throw&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// an exception, so this is a no-op.&lt;/span&gt;    &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;And that&amp;#39;s it! Fewer than 50 lines of code required, and nothing complicated at all. The interesting behaviour is all due to the way the C# compiler &lt;em&gt;uses&lt;/em&gt; the coordinator when awaiting it.&lt;/p&gt;  &lt;p&gt;We need AsyncVoidMethodBuilder as before, as we have some async void methods - but that doesn&amp;#39;t need to do anything significant. That&amp;#39;s basically all the code required to implement these basic round-robin coroutines.&lt;/p&gt;  &lt;h3&gt;Conclusion&lt;/h3&gt;  &lt;p&gt;Our first foray into the weird and wonderful world of coroutines was relatively tame. The basic idea of a coordinator keeping track of the state of all the different coroutines in one sense or another will keep coming back to us, but with different ways of controlling the execution flow.&lt;/p&gt;  &lt;p&gt;Next time we&amp;#39;ll see some coroutines which can pass data to each other.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1795089" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/TRwK6BPlGqE" height="1" width="1"/&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Wacky+Ideas/default.aspx">Wacky Ideas</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+5/default.aspx">C# 5</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/async/default.aspx">async</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Eduasync/default.aspx">Eduasync</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2011/06/22/eduasync-part-13-first-look-at-coroutines-with-async.aspx</feedburner:origLink></item><item><title>Eduasync part 12: Observing all exceptions</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/zyybvIYgbfM/eduasync-part-12-observing-all-exceptions.aspx</link><pubDate>Wed, 22 Jun 2011 06:04:20 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1795042</guid><dc:creator>skeet</dc:creator><slash:comments>10</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1795042</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1795042</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2011/06/22/eduasync-part-12-observing-all-exceptions.aspx#comments</comments><description>&lt;p&gt;(This post covers projects 16 and 17 in the &lt;a href="http://eduasync.googlecode.com/"&gt;source code&lt;/a&gt;.) &lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2011/06/22/eduasync-part-11-more-sophisticated-but-lossy-exception-handling.aspx"&gt;Last time&lt;/a&gt; we looked at unwrapping an AggregateException when we await a result. While there are potentially other interesting things we could look at with respect to exceptions (particularly around cancellation) I&amp;#39;m just going to touch on &lt;em&gt;one&lt;/em&gt; extra twist that the async CTP implements before I move on to some weird ways of using async.&lt;/p&gt;  &lt;h3&gt;TPL and unobserved exceptions&lt;/h3&gt;  &lt;p&gt;The Task Parallel Library (TPL) on which the async support is based has some interesting behaviour around exceptions. Just as it’s entirely possible for more than one thing to go wrong with a particular task, it&amp;#39;s &lt;em&gt;also&lt;/em&gt; quite easy to miss some errors, if you&amp;#39;re not careful.&lt;/p&gt;  &lt;p&gt;Here&amp;#39;s a simple example of an async method in C# 5 where we create two tasks, both of which will throw exceptions:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;async&lt;/span&gt; Task&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt; CauseTwoFailures()     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Task&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt; firstTask = Task&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt;.Factory.StartNew(() =&amp;gt; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;throw&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; InvalidOperationException();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; });     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Task&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt; secondTask = Task&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt;.Factory.StartNew(() =&amp;gt; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;throw&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; InvalidOperationException();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; });     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ValueType"&gt;int&lt;/span&gt; firstValue = &lt;span class="Modifier"&gt;await&lt;/span&gt; firstTask;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ValueType"&gt;int&lt;/span&gt; secondValue = &lt;span class="Modifier"&gt;await&lt;/span&gt; secondTask;     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; firstValue + secondValue;     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;Now the timing of the two tasks is actually irrelevant here. The first task will &lt;em&gt;always&lt;/em&gt; throw an exception, which means we&amp;#39;re never going to &lt;em&gt;await&lt;/em&gt; the second task. That means there&amp;#39;s never any code which asks for the second task&amp;#39;s result, or adds a continuation to it. It&amp;#39;s alone and unloved in a cruel world, with no-one to observe the exception it throws.&lt;/p&gt;  &lt;p&gt;If we call this method from the Eduasync code we&amp;#39;ve got at the moment, and wait for long enough (I&amp;#39;ve got a call to &lt;a href="http://msdn.microsoft.com/en-us/library/system.gc.waitforpendingfinalizers.aspx"&gt;GC.WaitForPendingFinalizers&lt;/a&gt; in the same code) the program will abort, with this error:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Unhandled Exception: System.AggregateException: A Task&amp;#39;s exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. ---&amp;gt; System.InvalidOperationException: Operation is not valid due to the current state of the object.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Ouch. The TPL takes a hard line on unobserved exceptions. They indicate failures (presumably) which you&amp;#39;ll never find out about until you start caring about the result of a task. Basically there are various ways of &amp;quot;observing&amp;quot; a task&amp;#39;s failure, whether by performing some act which causes it to be thrown (usually as part of an AggregateException) or just asking for the exception for a task which is known to be faulted. An unobserved exception will throw an InvalidOperationException in its finalizer, usually causing the process to exit.&lt;/p&gt;  &lt;p&gt;That works well in &amp;quot;normal&amp;quot; TPL code, where you&amp;#39;re explicitly managing tasks - but it&amp;#39;s not so handy in async, where perfectly reasonable looking code which starts a few tasks and then awaits them one at a time (possibly doing some processing in between) might hide an unobserved exception.&lt;/p&gt;  &lt;h3&gt;Observing all exceptions&lt;/h3&gt;  &lt;p&gt;Fortunately TPL provides a way of us to get out of the normal task behaviour. There&amp;#39;s an event &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler.unobservedtaskexception.aspx"&gt;TaskScheduler.UnobservedTaskException&lt;/a&gt; which is fired by the finalizer before it goes bang. The handlers of the event are allowed to observe the exception using &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.unobservedtaskexceptioneventargs.setobserved.aspx"&gt;UnobservedTaskExceptionEventArgs.SetObserved&lt;/a&gt; and can also check whether it&amp;#39;s &lt;em&gt;already&lt;/em&gt; been observed.&lt;/p&gt;  &lt;p&gt;So all we have to do is add a handler for the event and our program doesn&amp;#39;t crash any more:&lt;/p&gt;  &lt;div class="code"&gt;TaskScheduler.UnobservedTaskException += (sender, e) =&amp;gt;    &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Saving the day! This exception would have been unobserved: {0}&amp;quot;&lt;/span&gt;,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.Exception);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; e.SetObserved();     &lt;br /&gt;};&lt;/div&gt;  &lt;p&gt;In Eduasync this is currently &lt;em&gt;only&lt;/em&gt; performed explicitly, in project 17. In the async CTP something like this is performed as part of the type initializer for AsyncTaskMethodBuilder&amp;lt;T&amp;gt;, which you can unfortunately tell because that type initializer &lt;a href="http://stackoverflow.com/questions/6353807"&gt;crashes when running under medium trust&lt;/a&gt;. (That issue will be fixed before the final release.)&lt;/p&gt;  &lt;h3&gt;Global changes&lt;/h3&gt;  &lt;p&gt;This approach has a very significant effect: it changes the &lt;em&gt;global&lt;/em&gt; behaviour of the system. If you have a system which uses the TPL and you &lt;em&gt;want&lt;/em&gt; the existing .NET 4 behaviour of the process terminating when you have unobserved exceptions, you basically can&amp;#39;t use async at all - and if you use any code which does, you&amp;#39;ll see the more permissive behaviour.&lt;/p&gt;  &lt;p&gt;You could &lt;em&gt;potentially&lt;/em&gt; add your own event handler which aborted the application forcibly, but that&amp;#39;s not terribly nice either. You should quite possibly add a handler to at least &lt;em&gt;log&lt;/em&gt; these exceptions, so you can find out what&amp;#39;s been going wrong that you haven&amp;#39;t noticed.&lt;/p&gt;  &lt;p&gt;Of course, this only affects &lt;em&gt;unobserved&lt;/em&gt; exceptions - anything you&amp;#39;re already observing will not be affected. Still, it&amp;#39;s a pretty big change. I wouldn&amp;#39;t be surprised if this aspect of the behaviour of async in C# 5 changed before release; it feels to me like it isn&amp;#39;t quite right yet. Admittedly I&amp;#39;m not sure &lt;em&gt;how&lt;/em&gt; I would suggest changing it, but effectively reversing the existing behaviour goes against Microsoft&amp;#39;s past behaviour when it comes to backwards compatibility. Watch this space.&lt;/p&gt;  &lt;h3&gt;Conclusion&lt;/h3&gt;  &lt;p&gt;It&amp;#39;s worth pondering this whole issue yourself (and the one from last time), and making your feelings known to the team. I think it&amp;#39;s symptomatic of a wider problem in software engineering: we&amp;#39;re not really very good at handling errors. Java&amp;#39;s approach of checked exceptions didn&amp;#39;t turn out too well in my view, but the &amp;quot;anything goes&amp;quot; approach of C# has problems too... and introducing alternative models like the one in TPL makes things even more complicated. I don&amp;#39;t have any smart answers here - just that it&amp;#39;s something I&amp;#39;d like wiser people than myself to think about further.&lt;/p&gt;  &lt;p&gt;Next, we&amp;#39;re going to move a bit further away from the &amp;quot;normal&amp;quot; ways of using async, into the realm of &lt;a href="http://en.wikipedia.org/wiki/Coroutine"&gt;coroutines&lt;/a&gt;. This series is going to get increasingly obscure and silly, all in the name of really getting a feeling for the underlying execution model of async, before it possibly returns to more sensible topics such as task composition.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1795042" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/zyybvIYgbfM" height="1" width="1"/&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+5/default.aspx">C# 5</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/async/default.aspx">async</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Eduasync/default.aspx">Eduasync</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2011/06/22/eduasync-part-12-observing-all-exceptions.aspx</feedburner:origLink></item><item><title>Eduasync part 11: More sophisticated (but lossy) exception handling</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/A6N5ndsYKnw/eduasync-part-11-more-sophisticated-but-lossy-exception-handling.aspx</link><pubDate>Wed, 22 Jun 2011 05:25:46 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1795039</guid><dc:creator>skeet</dc:creator><slash:comments>10</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1795039</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1795039</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2011/06/22/eduasync-part-11-more-sophisticated-but-lossy-exception-handling.aspx#comments</comments><description>&lt;p&gt;(This post covers projects 13-15 in the &lt;a href="http://code.google.com/p/eduasync/source/browse/"&gt;source code&lt;/a&gt;.)&lt;/p&gt;  &lt;p&gt;Long-time readers of this blog may not learn much from this post - it&amp;#39;s mostly going over what I&amp;#39;ve &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2010/11/03/propagating-multiple-async-exceptions-or-not.aspx"&gt;covered before&lt;/a&gt;. Still, it&amp;#39;s new to Eduasync.&lt;/p&gt;  &lt;h3&gt;Why isn&amp;#39;t my exception being caught properly?&lt;/h3&gt;  &lt;p&gt;Exceptions are inherently problematic in C# 5. There are two conflicting aspects:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The point of the async feature in C# 5 is that you can write code which &lt;em&gt;mostly&lt;/em&gt; looks like its synchronous code. We expect to be able to catch specific exception types as normal. &lt;/li&gt;    &lt;li&gt;Asynchronous code may potentially have &lt;em&gt;multiple&lt;/em&gt; exceptions &amp;quot;at the same time&amp;quot;. The language simply isn&amp;#39;t designed to deal with that in the synchronous case. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Now if the language had been designed for asynchrony to start with, perhaps exception flow would have been designed differently - but we are where we are, and we all expect exceptions to work in a certain way.&lt;/p&gt;  &lt;p&gt;Let&amp;#39;s make all of this concrete with a sample:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; Main(&lt;span class="ReferenceType"&gt;string&lt;/span&gt;[] args)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Task&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt; task = FetchOrDefaultAsync();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Result: {0}&amp;quot;&lt;/span&gt;, task.Result);     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;async&lt;/span&gt; Task&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt; FetchOrDefaultAsync()     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Nothing special about IOException here&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;try&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Task&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt; fetcher = Task&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt;.Factory.StartNew(() =&amp;gt; { &lt;span class="Statement"&gt;throw&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; IOException(); });     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;await&lt;/span&gt; fetcher;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;catch&lt;/span&gt; (IOException e)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Caught IOException: {0}&amp;quot;&lt;/span&gt;, e);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; 5;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;catch&lt;/span&gt; (Exception e)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Caught arbitrary exception: {0}&amp;quot;&lt;/span&gt;, e);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; 10;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;Here we have a task which will throw an IOException, and some code which awaits that task - and has a catch block for IOException.&lt;/p&gt;  &lt;p&gt;So, what would &lt;em&gt;you&lt;/em&gt; expect this to print? With the code we&amp;#39;ve got in Eduasync so far, we get this:&lt;/p&gt;  &lt;div class="code"&gt;Caught arbitrary exception: System.AggregateException: One or more errors occurred. ---&amp;gt; System.IO.IOException: I/O error occurred.    &lt;br /&gt;...     &lt;br /&gt;Result: 10&lt;/div&gt;  &lt;p&gt;If you run the same code against the async CTP, you get this:&lt;/p&gt;  &lt;div class="code"&gt;Caught IOException: System.IO.IOException: I/O error occurred.    &lt;br /&gt;...     &lt;br /&gt;Result: 5 &lt;/div&gt;  &lt;p&gt;Hmm... we&amp;#39;re not behaving as per the CTP, and we&amp;#39;re not behaving as we&amp;#39;d really expect the normal synchronous code to behave.&lt;/p&gt;  &lt;p&gt;The first thing to work out is which boundary we should be fixing. In this case, the problem is between the async method and the task we&amp;#39;re awaiting, so the code we need to fix is TaskAwaiter&amp;lt;T&amp;gt;.&lt;/p&gt;  &lt;h3&gt;Handling AggregateException in TaskAwaiter&amp;lt;T&amp;gt;&lt;/h3&gt;  &lt;p&gt;Before we can fix it, we need to work out what&amp;#39;s going on. The stack trace I hid before actually show this reasonably clearly, with this section:&lt;/p&gt;  &lt;div class="code"&gt;at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceled Exceptions)    &lt;br /&gt;at System.Threading.Tasks.Task`1.get_Result()     &lt;br /&gt;at Eduasync.TaskAwaiter`1.GetResult() ...\TaskAwaiter.cs:line 40     &lt;br /&gt;at Eduasync.Program.&amp;lt;FetchOrDefaultAsync&amp;gt;d__2.MoveNext() ...\Program.cs:line 37 &lt;/div&gt;  &lt;p&gt;So AggregateException is being thrown by Task&amp;lt;T&amp;gt;.Result, which we&amp;#39;re calling from TaskAwaiter&amp;lt;T&amp;gt;.GetResult(). The documentation for &lt;a href="http://msdn.microsoft.com/en-us/library/dd321468.aspx"&gt;Task&amp;lt;T&amp;gt;.Result&lt;/a&gt; isn&amp;#39;t actually terribly revealing here, but the fact that &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.exception.aspx"&gt;Task&amp;lt;T&amp;gt;.Exception&lt;/a&gt; is of type &lt;a href="http://msdn.microsoft.com/en-us/library/system.aggregateexception.aspx"&gt;AggregateException&lt;/a&gt; is fairly revealing.&lt;/p&gt;  &lt;p&gt;Basically, the Task Parallel Library is built with the idea of multiple exceptions in mind - whereas our async method isn&amp;#39;t.&lt;/p&gt;  &lt;p&gt;Now the team in Microsoft &lt;em&gt;could&lt;/em&gt; have decided that really you should catch AggregateException and iterate over all the exceptions contained inside the exception, handling each of them separately. However, in &lt;em&gt;most&lt;/em&gt; cases that isn&amp;#39;t really practical - because in &lt;em&gt;most&lt;/em&gt; cases there will only be one exception (if any) and all that looping is relatively painful. They decided to simply extract the &lt;em&gt;first&lt;/em&gt; exception from the AggregateException within a task, and throw that instead.&lt;/p&gt;  &lt;p&gt;We can do that ourselves in TaskAwaiter&amp;lt;T&amp;gt;, like this:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Statement"&gt;try&lt;/span&gt;     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; task.Result;     &lt;br /&gt;}     &lt;br /&gt;&lt;span class="Statement"&gt;catch&lt;/span&gt; (AggregateException aggregate)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;if&lt;/span&gt; (aggregate.InnerExceptions.Count &amp;gt; 0)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Loses the proper stack trace. Oops. For workarounds, see&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// See http://bradwilson.typepad.com/blog/2008/04/small-decisions.html&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;throw&lt;/span&gt; aggregate.InnerExceptions[0];     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;else&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Nothing better to do, really...&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;throw&lt;/span&gt;;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;As you can tell from the comment, we end up losing the stack trace using this code. I don&amp;#39;t know exactly how the stack trace is preserved in the real Async CTP, but it is. I suspect this is done in a relatively obscure way at the moment - it&amp;#39;s possible that for .NET 5, there&amp;#39;ll be a cleaner way that all code can take advantage of.&lt;/p&gt;  &lt;p&gt;This code is also pretty ugly, catching the exception only to rethrow it. We can check whether or not the task has faulted using &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.status.aspx"&gt;Task&amp;lt;T&amp;gt;.Status&lt;/a&gt; and extract the AggregateException using &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.exception.aspx"&gt;Task&amp;lt;T&amp;gt;.Exception&lt;/a&gt; instead of forcing it to be thrown and catching it. We&amp;#39;ll see an example of that in a minute.&lt;/p&gt;  &lt;p&gt;With our new code in place, we can catch the IOException in our async code very easily.&lt;/p&gt;  &lt;h3&gt;What if I want all the exceptions?&lt;/h3&gt;  &lt;p&gt;In certain circumstances it really makes sense to collect multiple exceptions. This is particularly true when you&amp;#39;re waiting for multiple tasks to complete, e.g. with TaskEx.WhenAll in the Async CTP. This caused me a certain amount of concern for a while, but when Mads came to visit in late 2010 and we talked it over, we realized we could use the compositional nature of Task&amp;lt;T&amp;gt; and the convenience of TaskCompletionSource to implement an extension method preserving all the exceptions.&lt;/p&gt;  &lt;p&gt;As we&amp;#39;ve seen, when a task is awaited, its AggregateException is unwrapped, and the first exception rethrown. So if we create a &lt;em&gt;new&lt;/em&gt; task which adds an &lt;em&gt;extra&lt;/em&gt; layer of wrapping and make our code await that instead, only the extra layer will be unwrapped by the task awaiter, leaving the original AggregateException. To come up with a new task which &amp;quot;looks like&amp;quot; an existing task, we can simply create a TaskCompletionSource, add a continuation to the original task, and return the completion source&amp;#39;s task as the wrapper. When the continuation fires we&amp;#39;ll set the appropriate result on the completion source - cancellation, an exception, or the successful result.&lt;/p&gt;  &lt;p&gt;You may expect that we&amp;#39;d have to create a new AggregateException ourselves - but TaskCompletionSource.SetException will already do this for us. This makes it &lt;em&gt;looks&lt;/em&gt; like the code below isn&amp;#39;t performing any wrapping at all, but remember that Task&amp;lt;T&amp;gt;.Exception is already an AggregateException, and calling TaskCompletionSource.SetException will wrap it in &lt;em&gt;another&lt;/em&gt; AggregateException. Here&amp;#39;s the extension method in question:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt; Task&amp;lt;T&amp;gt; WithAllExceptions&amp;lt;T&amp;gt;(&lt;span class="Keyword"&gt;this&lt;/span&gt; Task&amp;lt;T&amp;gt; task)     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; TaskCompletionSource&amp;lt;T&amp;gt; tcs = &lt;span class="Keyword"&gt;new&lt;/span&gt; TaskCompletionSource&amp;lt;T&amp;gt;();     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; task.ContinueWith(ignored =&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;switch&lt;/span&gt; (task.Status)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;case&lt;/span&gt; TaskStatus.Canceled:     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; tcs.SetCanceled();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;break&lt;/span&gt;;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;case&lt;/span&gt; TaskStatus.RanToCompletion:     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; tcs.SetResult(task.Result);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;break&lt;/span&gt;;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;case&lt;/span&gt; TaskStatus.Faulted:     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// SetException will automatically wrap the original AggregateException&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// in another one. The new wrapper will be removed in TaskAwaiter, leaving&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// the original intact.&lt;/span&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; tcs.SetException(task.Exception);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;break&lt;/span&gt;;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;default&lt;/span&gt;:     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; tcs.SetException(&lt;span class="Keyword"&gt;new&lt;/span&gt; InvalidOperationException(&lt;span class="String"&gt;&amp;quot;Continuation called illegally.&amp;quot;&lt;/span&gt;));     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;break&lt;/span&gt;;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; });     &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; tcs.Task;     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;Here you can see the cleaner way of reacting to a task&amp;#39;s status - we don&amp;#39;t just try to fetch the result and catch any exceptions; we handle each status individually.&lt;/p&gt;  &lt;p&gt;I don&amp;#39;t know offhand what task scheduler is used for this continuation - it may be that we&amp;#39;d really want to specify the current task scheduler for a production-ready version of this code. However, the core idea is sound.&lt;/p&gt;  &lt;p&gt;It&amp;#39;s easy to &lt;em&gt;use&lt;/em&gt; this extension method within an async method, as shown here:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;async&lt;/span&gt; Task&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt; AwaitMultipleFailures()    &lt;br /&gt;{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;try&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;await&lt;/span&gt; CauseMultipleFailures().WithAllExceptions();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;catch&lt;/span&gt; (AggregateException e)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Caught arbitrary exception: {0}&amp;quot;&lt;/span&gt;, e);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; e.InnerExceptions.Count;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Nothing went wrong, remarkably!&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; 0;    &lt;br /&gt;}    &lt;br /&gt;    &lt;br /&gt;&lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt; Task&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt; CauseMultipleFailures()    &lt;br /&gt;{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="InlineComment"&gt;// Simplest way of inducing multiple exceptions&lt;/span&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Exception[] exceptions = { &lt;span class="Keyword"&gt;new&lt;/span&gt; IOException(), &lt;span class="Keyword"&gt;new&lt;/span&gt; ArgumentException() };    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; TaskCompletionSource&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt; tcs = &lt;span class="Keyword"&gt;new&lt;/span&gt; TaskCompletionSource&amp;lt;&lt;span class="ValueType"&gt;int&lt;/span&gt;&amp;gt;();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; tcs.SetException(exceptions);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; tcs.Task;    &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;Note that this will work perfectly well with the Async CTP and should be fine with the full release as well. I wouldn&amp;#39;t be entirely surprised to find something similar provided by the framework itself by release time, too.&lt;/p&gt;  &lt;h3&gt;Conclusion&lt;/h3&gt;  &lt;p&gt;It&amp;#39;s worth being aware of the impedance mismatch between the TPL and async methods in C# 5, as well as how this mismatch is handled. I dislike the idea of data loss, but I can see why it&amp;#39;s being handled in this way. It&amp;#39;s very much in line with the approach of trying to make asynchronous methods &lt;em&gt;look&lt;/em&gt; like synchronous ones as far as possible.&lt;/p&gt;  &lt;p&gt;We&amp;#39;ll probably look at the compositional nature of tasks again later in the series, but this was one simple example of how transparent it can be - a simple extension method can change the behaviour to avoid the risk of losing exception information when you&amp;#39;re &lt;em&gt;expecting&lt;/em&gt; that multiple things can go wrong.&lt;/p&gt;  &lt;p&gt;It&amp;#39;s worth remembering that this behaviour is very specific to Task and Task&amp;lt;T&amp;gt;, and the awaiter types associated with them. If you&amp;#39;re awaiting other types of expressions, they may behave differently with respect to exceptions.&lt;/p&gt;  &lt;p&gt;Before we leave the topic of exceptions, there&amp;#39;s one other aspect we need to look at - what happens when an exception isn&amp;#39;t &lt;em&gt;observed&lt;/em&gt;...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1795039" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/A6N5ndsYKnw" height="1" width="1"/&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_+5/default.aspx">C# 5</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/async/default.aspx">async</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Eduasync/default.aspx">Eduasync</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2011/06/22/eduasync-part-11-more-sophisticated-but-lossy-exception-handling.aspx</feedburner:origLink></item><item><title>LINQ To Objects and the performance of nested "Where" calls</title><link>http://feedproxy.google.com/~r/JonSkeetCodingBlog/~3/P6gnvqhxDGA/linq-to-objects-and-the-performance-of-nested-quot-where-quot-calls.aspx</link><pubDate>Thu, 16 Jun 2011 18:19:12 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1794830</guid><dc:creator>skeet</dc:creator><slash:comments>18</slash:comments><wfw:commentRss>http://msmvps.com/blogs/jon_skeet/rsscomments.aspx?PostID=1794830</wfw:commentRss><wfw:comment>http://msmvps.com/blogs/jon_skeet/commentapi.aspx?PostID=1794830</wfw:comment><comments>http://msmvps.com/blogs/jon_skeet/archive/2011/06/16/linq-to-objects-and-the-performance-of-nested-quot-where-quot-calls.aspx#comments</comments><description>&lt;p&gt;This post came out of this &lt;a href="http://stackoverflow.com/questions/6359980"&gt;Stack Overflow&lt;/a&gt; question, which essentially boils down to which is better out of these two options:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Linq"&gt;var&lt;/span&gt; oneBigPredicate = collection.Where(x =&amp;gt; Condition1(x)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;amp;&amp;amp; Condition2(x)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;amp;&amp;amp; Condition3(x));     &lt;br /&gt;    &lt;br /&gt;&lt;span class="Linq"&gt;var&lt;/span&gt; multiplePredicates = collection.Where(x =&amp;gt; Condition1(x))     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Where(x =&amp;gt; Condition2(x))     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Where(x =&amp;gt; Condition3(x)) &lt;/div&gt;  &lt;p&gt;The first case is logically a single &amp;quot;wrapper&amp;quot; sequence around the original collection, with a filter which checks all three conditions (but applying short-circuiting logic, of course) before the wrapper will yield an item from the original sequence.&lt;/p&gt;  &lt;p&gt;The second case is logically a set of concentric wrapper sequences, each applying a filter which checks a single condition. Asking the &amp;quot;outer&amp;quot; wrapper for the next item involves that one asking the &amp;quot;middle&amp;quot; wrapper for the next item, which asks the &amp;quot;inner&amp;quot; wrapper for the next item, which asks the original collection for the next item... the item is then passed &amp;quot;outwards&amp;quot; through the wrappers, with the filters being applied as we go, of course.&lt;/p&gt;  &lt;p&gt;Now the two will achieve the same result, and I should say up-front that in most realistic cases, it won&amp;#39;t make a significant difference which you use. But realistic cases aren&amp;#39;t nearly as interesting to investigate as pathological cases, so I decided to benchmark a few different options for the second case. In particular, I wanted to find out how long it took to iterate over a query in the cases where the condition was either &amp;quot;always true&amp;quot; or &amp;quot;always false&amp;quot; - and vary the depth of nesting. Note that I&amp;#39;m &lt;em&gt;not&lt;/em&gt; actually testing the first kind of query shown above... I suspect it wouldn&amp;#39;t be terribly interesting, at least compared with the results of the second query.&lt;/p&gt;  &lt;p&gt;The simplest way of creating a &amp;quot;collection&amp;quot; of a large size is to use Enumerable.Repeat(), and the simplest way of iterating over the whole sequence is to just call Count() on it... so that&amp;#39;s what I do, timing how long the Count() call takes. (I don&amp;#39;t actually print out the results of Count(), but with an &amp;quot;always false&amp;quot; predicate I&amp;#39;ll get 0, and with an &amp;quot;always true&amp;quot; predicate I&amp;#39;ll get the size of the input collection.)&lt;/p&gt;  &lt;p&gt;Here&amp;#39;s the sample code:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Namespace"&gt;using&lt;/span&gt; System;     &lt;br /&gt;&lt;span class="Namespace"&gt;using&lt;/span&gt; System.Collections.Generic;     &lt;br /&gt;&lt;span class="Namespace"&gt;using&lt;/span&gt; System.Diagnostics;     &lt;br /&gt;&lt;span class="Namespace"&gt;using&lt;/span&gt; System.Linq;     &lt;br /&gt;    &lt;br /&gt;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Test     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; Main()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="ValueType"&gt;int&lt;/span&gt; size = 10000000;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Always false&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; RunTests(size, x =&amp;gt; &lt;span class="Keyword"&gt;false&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Always true&amp;quot;&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; RunTests(size, x =&amp;gt; &lt;span class="Keyword"&gt;true&lt;/span&gt;);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; RunTests(&lt;span class="ValueType"&gt;int&lt;/span&gt; size, Func&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;, &lt;span class="ValueType"&gt;bool&lt;/span&gt;&amp;gt; predicate)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;for&lt;/span&gt; (&lt;span class="ValueType"&gt;int&lt;/span&gt; i = 1; i &amp;lt;= 10; i++)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; RunTest(i, size, predicate);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ValueType"&gt;void&lt;/span&gt; RunTest(&lt;span class="ValueType"&gt;int&lt;/span&gt; depth, &lt;span class="ValueType"&gt;int&lt;/span&gt; size, Func&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;, &lt;span class="ValueType"&gt;bool&lt;/span&gt;&amp;gt; predicate)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; IEnumerable&amp;lt;&lt;span class="ReferenceType"&gt;string&lt;/span&gt;&amp;gt; input = Enumerable.Repeat(&lt;span class="String"&gt;&amp;quot;value&amp;quot;&lt;/span&gt;, size);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;for&lt;/span&gt; (&lt;span class="ValueType"&gt;int&lt;/span&gt; i = 0; i &amp;lt; depth; i++)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; input = input.Where(predicate);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Stopwatch sw = Stopwatch.StartNew();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; input.Count();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; sw.Stop();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Console.WriteLine(&lt;span class="String"&gt;&amp;quot;Depth: {0} Size: {1} Time: {2}ms&amp;quot;&lt;/span&gt;,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; depth, size, sw.ElapsedMilliseconds);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;You may notice there&amp;#39;s no JIT warm-up here - but I&amp;#39;ve tried that, and it doesn&amp;#39;t alter the results much. Likewise I&amp;#39;ve also tried with a larger size of collection, and the trend is the same - and the trend is the interesting bit.&lt;/p&gt;  &lt;h3&gt;Time to guess the results...&lt;/h3&gt;  &lt;p&gt;Before I include the results, I&amp;#39;ll explain what I &lt;em&gt;thought&lt;/em&gt; would happen. I had the mental model I described before, with multiple sequences feeding each other.&lt;/p&gt;  &lt;p&gt;When the condition is always false, I&amp;#39;d expected the call to MoveNext() from Count() to get all the way to the innermost filtering sequence, which would then iterate over all of the input collection, applying the filter on each item and never yielding a result. That innermost MoveNext() call returns false, and that propagates right back to Count(). All the (significant) time is spent in the innermost loop, testing and rejecting items. That shouldn&amp;#39;t depend on the depth of the nesting we&amp;#39;ve got, right? I&amp;#39;d expect it to be linear in terms of the size of the collection, but constant in terms of nesting. We&amp;#39;ll see.&lt;/p&gt;  &lt;p&gt;When the condition is always true, we&amp;#39;re in a much worse situation. We need to propagate every item from the collection through all the filters, out to Count(), checking the condition and accepting the item on each check. Each MoveNext() call has to go all the way to the innermost filter, then the result has to propagate out again. That sounds like it should be roughly linear in the depth of the nesting, as well as still being linear in the size of the collection - assuming a very simplistic execution model, of course.&lt;/p&gt;  &lt;p&gt;Before you look at the results, check that you understand my logic, and see if you can think why it might not be the case.&lt;/p&gt;  &lt;h3&gt;The actual results&lt;/h3&gt;  &lt;p&gt;Here&amp;#39;s what I&amp;#39;ve actually observed, with all times in milliseconds. The size of the collection is constant - we&amp;#39;re only varying the depth&lt;/p&gt;  &lt;table border="1" cellspacing="0" cellpadding="2"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td align="right"&gt;&lt;strong&gt;Depth&lt;/strong&gt;&lt;/td&gt;        &lt;td align="right"&gt;&lt;strong&gt;Time for &amp;quot;always false&amp;quot;&lt;/strong&gt;&lt;/td&gt;        &lt;td align="right"&gt;&lt;strong&gt;Time for &amp;quot;always true&amp;quot;&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;1&lt;/td&gt;        &lt;td&gt;182&lt;/td&gt;        &lt;td&gt;305&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;2&lt;/td&gt;        &lt;td&gt;219&lt;/td&gt;        &lt;td&gt;376&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;3&lt;/td&gt;        &lt;td&gt;246&lt;/td&gt;        &lt;td&gt;452&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;4&lt;/td&gt;        &lt;td&gt;305&lt;/td&gt;        &lt;td&gt;548&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;5&lt;/td&gt;        &lt;td&gt;350&lt;/td&gt;        &lt;td&gt;650&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;6&lt;/td&gt;        &lt;td&gt;488&lt;/td&gt;        &lt;td&gt;709&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;7&lt;/td&gt;        &lt;td&gt;480&lt;/td&gt;        &lt;td&gt;795&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;8&lt;/td&gt;        &lt;td&gt;526&lt;/td&gt;        &lt;td&gt;880&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;9&lt;/td&gt;        &lt;td&gt;583&lt;/td&gt;        &lt;td&gt;996&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;10&lt;/td&gt;        &lt;td&gt;882&lt;/td&gt;        &lt;td&gt;1849&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;There are a few things to note here:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The &amp;quot;always true&amp;quot; time is going up broadly linearly, as we&amp;#39;d expected &lt;/li&gt;    &lt;li&gt;The &amp;quot;always false&amp;quot; time is &lt;em&gt;also&lt;/em&gt; going going up broadly linearly, even though we&amp;#39;d expected it to be roughly constant &lt;/li&gt;    &lt;li&gt;The &amp;quot;always false&amp;quot; time is still significantly better than the &amp;quot;always true&amp;quot; time, which is somewhat reassuring &lt;/li&gt;    &lt;li&gt;The performance at a depth of 10 is significantly worse than at a depth of 9 in both cases &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Okay, so that&amp;#39;s bizarre. What can possibly be making the time taken by the &amp;quot;inner&amp;quot; filtering sequence go up with the depth of the nesting? It shouldn&amp;#39;t &lt;em&gt;know&lt;/em&gt; about the rest of the calls to Where - that&amp;#39;s not its job. Time to investigate...&lt;/p&gt;  &lt;h3&gt;Reimplementing Where&lt;/h3&gt;  &lt;p&gt;As you &lt;a href="http://msmvps.com/blogs/jon_skeet/archive/2010/09/03/reimplementing-linq-to-objects-part-2-quot-where-quot.aspx"&gt;may have seen before&lt;/a&gt;, Where isn&amp;#39;t very hard to implement - at least it&amp;#39;s not hard to implement if you&amp;#39;re not trying to be clever. In order to mess around with things to check that my mental model was correct, I decided to run exactly the same tests again, but against the Edulinq implementation. This time, the results are very different:&lt;/p&gt;  &lt;table border="1" cellspacing="0" cellpadding="2"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td align="right"&gt;&lt;strong&gt;Depth&lt;/strong&gt;&lt;/td&gt;        &lt;td align="right"&gt;&lt;strong&gt;Time for &amp;quot;always false&amp;quot;&lt;/strong&gt;&lt;/td&gt;        &lt;td align="right"&gt;&lt;strong&gt;Time for &amp;quot;always true&amp;quot;&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;1&lt;/td&gt;        &lt;td&gt;232&lt;/td&gt;        &lt;td&gt;502&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;2&lt;/td&gt;        &lt;td&gt;235&lt;/td&gt;        &lt;td&gt;950&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;3&lt;/td&gt;        &lt;td&gt;256&lt;/td&gt;        &lt;td&gt;1946&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;4&lt;/td&gt;        &lt;td&gt;229&lt;/td&gt;        &lt;td&gt;2571&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;5&lt;/td&gt;        &lt;td&gt;227&lt;/td&gt;        &lt;td&gt;3103&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;6&lt;/td&gt;        &lt;td&gt;226&lt;/td&gt;        &lt;td&gt;3535&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;7&lt;/td&gt;        &lt;td&gt;226&lt;/td&gt;        &lt;td&gt;3901&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;8&lt;/td&gt;        &lt;td&gt;232&lt;/td&gt;        &lt;td&gt;4365&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;9&lt;/td&gt;        &lt;td&gt;229&lt;/td&gt;        &lt;td&gt;4838&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;10&lt;/td&gt;        &lt;td&gt;226&lt;/td&gt;        &lt;td&gt;5219&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;Well look at that... suddenly our &amp;quot;always false&amp;quot; query has the expected characteristics. The &amp;quot;always true&amp;quot; query is basically linear except for the jump in time taken between a depth of 2 and a depth of 3. This may well be the same sort of discontinuity present in the earlier results, but at a different depth. (I&amp;#39;ll do more research on that another time, I think.)&lt;/p&gt;  &lt;p&gt;So if the naive implementation actually works &lt;em&gt;better&lt;/em&gt; in some cases, what&amp;#39;s going wrong in the &amp;quot;always false&amp;quot; case in the real LINQ to Objects? We can work this out by taking a stack trace from a predicate. Here&amp;#39;s a sample query which will throw an exception:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Linq"&gt;var&lt;/span&gt; query = Enumerable.Repeat(&lt;span class="String"&gt;&amp;quot;value&amp;quot;&lt;/span&gt;, 1)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Where(x =&amp;gt; { &lt;span class="Statement"&gt;throw&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; Exception(&lt;span class="String"&gt;&amp;quot;Bang!&amp;quot;&lt;/span&gt;); })     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Where(x =&amp;gt; &lt;span class="Keyword"&gt;true&lt;/span&gt;)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Where(x =&amp;gt; &lt;span class="Keyword"&gt;true&lt;/span&gt;)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Where(x =&amp;gt; &lt;span class="Keyword"&gt;true&lt;/span&gt;)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Where(x =&amp;gt; &lt;span class="Keyword"&gt;true&lt;/span&gt;)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Where(x =&amp;gt; &lt;span class="Keyword"&gt;true&lt;/span&gt;)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Where(x =&amp;gt; &lt;span class="Keyword"&gt;true&lt;/span&gt;)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Where(x =&amp;gt; &lt;span class="Keyword"&gt;true&lt;/span&gt;); &lt;/div&gt;  &lt;p&gt;When you try to count that query (or do anything else which will iterate over it) you get a stack trace like this:&lt;/p&gt;  &lt;div class="code"&gt;Unhandled Exception: System.Exception: Bang!    &lt;br /&gt;&amp;#160;&amp;#160; at Test.&amp;lt;Main&amp;gt;b__0(String x)     &lt;br /&gt;&amp;#160;&amp;#160; at System.Linq.Enumerable.&amp;lt;&amp;gt;c__DisplayClassf`1.&amp;lt;CombinePredicates&amp;gt;b__e(TSource x)     &lt;br /&gt;&amp;#160;&amp;#160; at System.Linq.Enumerable.&amp;lt;&amp;gt;c__DisplayClassf`1.&amp;lt;CombinePredicates&amp;gt;b__e(TSource x)     &lt;br /&gt;&amp;#160;&amp;#160; at System.Linq.Enumerable.&amp;lt;&amp;gt;c__DisplayClassf`1.&amp;lt;CombinePredicates&amp;gt;b__e(TSource x)     &lt;br /&gt;&amp;#160;&amp;#160; at System.Linq.Enumerable.&amp;lt;&amp;gt;c__DisplayClassf`1.&amp;lt;CombinePredicates&amp;gt;b__e(TSource x)     &lt;br /&gt;&amp;#160;&amp;#160; at System.Linq.Enumerable.&amp;lt;&amp;gt;c__DisplayClassf`1.&amp;lt;CombinePredicates&amp;gt;b__e(TSource x)     &lt;br /&gt;&amp;#160;&amp;#160; at System.Linq.Enumerable.&amp;lt;&amp;gt;c__DisplayClassf`1.&amp;lt;CombinePredicates&amp;gt;b__e(TSource x)     &lt;br /&gt;&amp;#160;&amp;#160; at System.Linq.Enumerable.&amp;lt;&amp;gt;c__DisplayClassf`1.&amp;lt;CombinePredicates&amp;gt;b__e(TSource x)     &lt;br /&gt;&amp;#160;&amp;#160; at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()     &lt;br /&gt;&amp;#160;&amp;#160; at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source)     &lt;br /&gt;&amp;#160;&amp;#160; at Test.Main() &lt;/div&gt;  &lt;p&gt;Ooh, look at that stack - we&amp;#39;ve got 8 Where clauses, and 7 levels of DisplayClassf`1 - which looks like a generated class, perhaps for a lambda expression.&lt;/p&gt;  &lt;p&gt;Between this and a helpful email from Eric Lippert, we can basically work out what&amp;#39;s going on. LINQ to Objects knows that it can combine two Where clauses by just constructing a compound filter. Just for kicks, let&amp;#39;s do the same thing - almost certainly more simplistically than LINQ to Objects - but in a way that will give the right idea:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="InlineComment"&gt;// We could have an interface for this, of course.&lt;/span&gt;     &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; FilteredEnumerable&amp;lt;T&amp;gt; : IEnumerable&amp;lt;T&amp;gt;     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; source;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; Func&amp;lt;T, &lt;span class="ValueType"&gt;bool&lt;/span&gt;&amp;gt; predicate;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;internal&lt;/span&gt; FilteredEnumerable(IEnumerable&amp;lt;T&amp;gt; source,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Func&amp;lt;T, &lt;span class="ValueType"&gt;bool&lt;/span&gt;&amp;gt; predicate)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Keyword"&gt;this&lt;/span&gt;.source = source;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Keyword"&gt;this&lt;/span&gt;.predicate = predicate;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt; IEnumerator&amp;lt;T&amp;gt; GetEnumerator()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;foreach&lt;/span&gt; (T item &lt;span class="Statement"&gt;in&lt;/span&gt; source)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;if&lt;/span&gt; (predicate(item))     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;yield&lt;/span&gt;&amp;#160;&lt;span class="Statement"&gt;return&lt;/span&gt; item;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; IEnumerator IEnumerable.GetEnumerator()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; GetEnumerator();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt; FilteredEnumerable&amp;lt;T&amp;gt; Where(Func&amp;lt;T, &lt;span class="ValueType"&gt;bool&lt;/span&gt;&amp;gt; extraPredicate)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; FilteredEnumerable&amp;lt;T&amp;gt;(source,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; x =&amp;gt; &lt;span class="Keyword"&gt;this&lt;/span&gt;.predicate(x) &amp;amp;&amp;amp; extraPredicate(x));     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;}     &lt;br /&gt;    &lt;br /&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; Extensions     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;static&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; Where&amp;lt;T&amp;gt;(&lt;span class="Keyword"&gt;this&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; source,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Func&amp;lt;T, &lt;span class="ValueType"&gt;bool&lt;/span&gt;&amp;gt; predicate)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Linq"&gt;var&lt;/span&gt; filtered = source &lt;span class="Keyword"&gt;as&lt;/span&gt; FilteredEnumerable&amp;lt;T&amp;gt;;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; filtered == &lt;span class="Keyword"&gt;null&lt;/span&gt; ? &lt;span class="Keyword"&gt;new&lt;/span&gt; FilteredEnumerable&amp;lt;T&amp;gt;(source, predicate)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; : filtered.Where(predicate);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;Let&amp;#39;s run the same tests as before, and see how we do...&lt;/p&gt;  &lt;table border="1" cellspacing="0" cellpadding="2"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td align="right"&gt;&lt;strong&gt;Depth&lt;/strong&gt;&lt;/td&gt;        &lt;td align="right"&gt;&lt;strong&gt;Time for &amp;quot;always false&amp;quot;&lt;/strong&gt;&lt;/td&gt;        &lt;td align="right"&gt;&lt;strong&gt;Time for &amp;quot;always true&amp;quot;&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;1&lt;/td&gt;        &lt;td&gt;240&lt;/td&gt;        &lt;td&gt;504&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;2&lt;/td&gt;        &lt;td&gt;275&lt;/td&gt;        &lt;td&gt;605&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;3&lt;/td&gt;        &lt;td&gt;332&lt;/td&gt;        &lt;td&gt;706&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;4&lt;/td&gt;        &lt;td&gt;430&lt;/td&gt;        &lt;td&gt;797&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;5&lt;/td&gt;        &lt;td&gt;525&lt;/td&gt;        &lt;td&gt;892&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;6&lt;/td&gt;        &lt;td&gt;558&lt;/td&gt;        &lt;td&gt;980&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;7&lt;/td&gt;        &lt;td&gt;638&lt;/td&gt;        &lt;td&gt;1085&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;8&lt;/td&gt;        &lt;td&gt;715&lt;/td&gt;        &lt;td&gt;1208&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;9&lt;/td&gt;        &lt;td&gt;802&lt;/td&gt;        &lt;td&gt;1343&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;10&lt;/td&gt;        &lt;td&gt;1312&lt;/td&gt;        &lt;td&gt;2768&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;Oh look - it feels like the real LINQ to Objects implementation. A bit slower, certainly, but that&amp;#39;s okay. The way it trends is the same. In particular, it&amp;#39;s slower than the naive implementation for the &amp;quot;always false&amp;quot; filter, but faster for the &amp;quot;always true&amp;quot; filter.&lt;/p&gt;  &lt;h3&gt;Could things be improved?&lt;/h3&gt;  &lt;p&gt;The problem here is the creation of nested delegates. We end up with a large stack (which &lt;em&gt;appears&lt;/em&gt; to be causing a bigger problem when it reaches a depth of 10) when really we want to just build another delegate.&lt;/p&gt;  &lt;p&gt;The thought occurs that we &lt;em&gt;could&lt;/em&gt; potentially use expression trees to do this. Not in a signature-compatible way with LINQ to Objects, but we should be able to combine (a =&amp;gt; a == 3) and (b =&amp;gt; b != 10) into (x =&amp;gt; x == 3 &amp;amp;&amp;amp; x != 10) effectively. Then when we&amp;#39;re asked to iterate, we just need to compile the expression tree to a delegate, and filter using that single, efficient delegate.&lt;/p&gt;  &lt;p&gt;There are three problems with this:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;It goes against the normal approach of LINQ to Objects, using delegates instead of expression trees. Heck, with expression trees throughout we could do all kinds of interesting optimizations. &lt;/li&gt;    &lt;li&gt;Creating and compiling the expression tree &lt;em&gt;may&lt;/em&gt; be more expensive than the iteration - we don&amp;#39;t really know. It depends on how large the collection is, etc. &lt;/li&gt;    &lt;li&gt;It&amp;#39;s somewhat complicated to implement, because we need to rewrite the constituent expressions; note how &amp;quot;a&amp;quot; and &amp;quot;b&amp;quot; both become &amp;quot;x&amp;quot; in the example above. This is the main reason I haven&amp;#39;t actually bothered trying this in order to benchmark it :) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;There are various other options to do with building a more efficient way of evaluating the predicates. One pretty simple (but repetitive) solution is to have one class per number of predicates, with a field per predicate - FilteredEnumerable1, FilteredEnumerable2 etc. When you get bored (e.g. FilteredEnumberable9) you construct any further ones by combining predicates as per the LINQ to Objects approach. For example, here&amp;#39;s an implementation of FilteredEnumerable3:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="Modifier"&gt;public&lt;/span&gt;&amp;#160;&lt;span class="ReferenceType"&gt;class&lt;/span&gt; FilteredEnumerable3&amp;lt;T&amp;gt; : IFilteredEnumerable&amp;lt;T&amp;gt;     &lt;br /&gt;{     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; source;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; Func&amp;lt;T, &lt;span class="ValueType"&gt;bool&lt;/span&gt;&amp;gt; predicate0;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; Func&amp;lt;T, &lt;span class="ValueType"&gt;bool&lt;/span&gt;&amp;gt; predicate1;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;private&lt;/span&gt;&amp;#160;&lt;span class="Modifier"&gt;readonly&lt;/span&gt; Func&amp;lt;T, &lt;span class="ValueType"&gt;bool&lt;/span&gt;&amp;gt; predicate2;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;internal&lt;/span&gt; FilteredEnumerable3(IEnumerable&amp;lt;T&amp;gt; source,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Func&amp;lt;T, &lt;span class="ValueType"&gt;bool&lt;/span&gt;&amp;gt; predicate0,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Func&amp;lt;T, &lt;span class="ValueType"&gt;bool&lt;/span&gt;&amp;gt; predicate1,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Func&amp;lt;T, &lt;span class="ValueType"&gt;bool&lt;/span&gt;&amp;gt; predicate2)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Keyword"&gt;this&lt;/span&gt;.source = source;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Keyword"&gt;this&lt;/span&gt;.predicate0 = predicate0;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Keyword"&gt;this&lt;/span&gt;.predicate1 = predicate1;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Keyword"&gt;this&lt;/span&gt;.predicate2 = predicate2;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt; IEnumerator&amp;lt;T&amp;gt; GetEnumerator()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;foreach&lt;/span&gt; (T item &lt;span class="Statement"&gt;in&lt;/span&gt; source)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;if&lt;/span&gt; (predicate0(item) &amp;amp;&amp;amp;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; predicate1(item) &amp;amp;&amp;amp;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; predicate2(item))     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;yield&lt;/span&gt;&amp;#160;&lt;span class="Statement"&gt;return&lt;/span&gt; item;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; IEnumerator IEnumerable.GetEnumerator()     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt; GetEnumerator();     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Modifier"&gt;public&lt;/span&gt; IFilteredEnumerable&amp;lt;T&amp;gt; Where(Func&amp;lt;T, &lt;span class="ValueType"&gt;bool&lt;/span&gt;&amp;gt; extraPredicate)     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Statement"&gt;return&lt;/span&gt;&amp;#160;&lt;span class="Keyword"&gt;new&lt;/span&gt; FilteredEnumerable4&amp;lt;T&amp;gt;(source,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; predicate0,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; predicate1,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; predicate2,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; extraPredicate);     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }     &lt;br /&gt;} &lt;/div&gt;  &lt;p&gt;In this case, I&amp;#39;m rather pleased with the results:&lt;/p&gt;  &lt;table border="1" cellspacing="0" cellpadding="2"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td align="right"&gt;&lt;strong&gt;Depth&lt;/strong&gt;&lt;/td&gt;        &lt;td align="right"&gt;&lt;strong&gt;Time for &amp;quot;always false&amp;quot;&lt;/strong&gt;&lt;/td&gt;        &lt;td align="right"&gt;&lt;strong&gt;Time for &amp;quot;always true&amp;quot;&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;1&lt;/td&gt;        &lt;td&gt;237&lt;/td&gt;        &lt;td&gt;504&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;2&lt;/td&gt;        &lt;td&gt;231&lt;/td&gt;        &lt;td&gt;551&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;3&lt;/td&gt;        &lt;td&gt;231&lt;/td&gt;        &lt;td&gt;599&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;4&lt;/td&gt;        &lt;td&gt;225&lt;/td&gt;        &lt;td&gt;625&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;5&lt;/td&gt;        &lt;td&gt;228&lt;/td&gt;        &lt;td&gt;703&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;6&lt;/td&gt;        &lt;td&gt;224&lt;/td&gt;        &lt;td&gt;787&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;7&lt;/td&gt;        &lt;td&gt;222&lt;/td&gt;        &lt;td&gt;876&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;8&lt;/td&gt;        &lt;td&gt;225&lt;/td&gt;        &lt;td&gt;966&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;9&lt;/td&gt;        &lt;td&gt;232&lt;/td&gt;        &lt;td&gt;1069&lt;/td&gt;     &lt;/tr&gt;      &lt;tr align="right"&gt;       &lt;td&gt;10&lt;/td&gt;        &lt;td&gt;219&lt;/td&gt;        &lt;td&gt;1191&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;Now that&amp;#39;s more like it. Other than a few cells, we&amp;#39;re actually outperforming LINQ to Objects - and still compatible with it. The only problem is that the cells where it&amp;#39;s slower are the common ones - the top few in the right hand column. And this was just going up to FilteredEnumerable4&amp;lt;T&amp;gt;.&lt;/p&gt;  &lt;p&gt;I honestly don&amp;#39;t know why my FilteredEnumerable1&amp;lt;T&amp;gt; is slower than whatever&amp;#39;s in LINQ to Objects. But it&amp;#39;s nice to see the rest going quickly... I suspect there are some downsides as well, and basically I trust the team to make the right decisions for normal cases.&lt;/p&gt;  &lt;h3&gt;And there&amp;#39;s more...&lt;/h3&gt;  &lt;p&gt;You may be surprised to hear I&amp;#39;ve kept things simple here - as Eric mentioned to me, it&amp;#39;s not just Where and Select that can be transformed in this way. Select works as well - you can combine projections together pretty easily. So imagine that we&amp;#39;ve effectively got this transformation:&lt;/p&gt;  &lt;div class="code"&gt;&lt;span class="InlineComment"&gt;// Normal LINQ query&lt;/span&gt;     &lt;br /&gt;&lt;span class="Linq"&gt;var&lt;/span&gt; query = list.Where(x =&amp;gt; Condition1(x))     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Where(x =&amp;gt; Condition2(x))     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Select(x =&amp;gt; Projection1(x))     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; .Select(y =&amp;gt; Projection2(y));     &lt;br /&gt;    &lt;br /&gt;&lt;span class="InlineComment"&gt;// After optimization&lt;/span&gt;     &lt;br /&gt;&lt;span class="Linq"&gt;var&lt;/span&gt; query = list.WhereSelect(x =&amp;gt; Condition1(x) &amp;amp;&amp;amp; Condition2(x),     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; x =&amp;gt; Projection2(Projection1(x)); &lt;/div&gt;  &lt;p&gt;Of course at that point, my FilteredEnumerableX&amp;lt;T&amp;gt; approach becomes even more unwieldy as you have two axes - the number of predicates and the number of projections.&lt;/p&gt;  &lt;h3&gt;Conclusion&lt;/h3&gt;  &lt;p&gt;As is so often the case with performance, there&amp;#39;s more to Where than there first appears. When I started benchmarking this I had &lt;em&gt;no idea&lt;/em&gt; what I was getting myself into. The great thing is that very few people would &lt;em&gt;ever&lt;/em&gt; need to know this. It&amp;#39;s all hidden by the abstraction.&lt;/p&gt;  &lt;p&gt;Still, isn&amp;#39;t this sort of thing fun?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1794830" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/JonSkeetCodingBlog/~4/P6gnvqhxDGA" height="1" width="1"/&gt;</description><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://msmvps.com/blogs/jon_skeet/archive/tags/Benchmarking/default.aspx">Benchmarking</category><feedburner:origLink>http://msmvps.com/blogs/jon_skeet/archive/2011/06/16/linq-to-objects-and-the-performance-of-nested-quot-where-quot-calls.aspx</feedburner:origLink></item></channel></rss>

