O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How Asynchronous Methods Execute

Because await expressions are just expressions, you can use them in (almost) all places where you can use expressions. For example, it’s perfectly fine to write the following:

string res = await Bar(await Foo() + await Qux(), 42 * await Baz());

This code evaluates as if you had written the following:

var __t1 = Foo();var __t2 = await __t1;var __t3 = Qux();var __t4 = await __t3;var __t5 = __t2 + __t4;var __t6 = Baz();var __t7 = await __t5;var __t8 = 42 * __t7;var __t9 = Bar(__t5, __t8);string res = await __t9;

In this example, the containing asynchronous method can be suspended on no less than four occasions, corresponding to each of the await expressions. However, the code still executes sequentially with a top-to-bottom ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required