Chapter 14. The Async Compiler Transform—in Depth
Async is implemented in the C# compiler with some help from the .NET
framework base class libraries. The runtime itself didn’t need any changes
to support async. That means await is
implemented by a transformation to something that we could have written
ourselves in earlier versions of C#. We can use a decompiler like .NET
Reflector to take a look at the generated code.
As well as being interesting, understanding the generated code is helpful for debugging, performance analysis, and other diagnostics on async code.
The stub Method
The async method is replaced by a stub method. The first thing that happens when you call an async method is that the stub method runs. Let’s look at this simple async method as an example:
publicasyncTask<int>AlexsMethod(){intfoo=3;awaitTask.Delay(500);returnfoo;}
The stub method generated by the compiler looks like this:
publicTask<int>AlexsMethod(){<AlexsMethod>d__0stateMachine=new<AlexsMethod>d__0();stateMachine.<>4__this=this;stateMachine.<>t__builder=AsyncTaskMethodBuilder<int>.Create();stateMachine.<>1__state=-1;stateMachine.<>t__builder.Start<<AlexsMethod>d__0>(refstateMachine);returnstateMachine.<>t__builder.Task;}
I’ve manually improved the names of the variables to make it easier to understand.
As we saw in Async, Method Signatures, and Interfaces, the
async keyword has no effect on how the method is used from the outside. That becomes obvious when you see that the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access