---
Id: 56bea812-02f1-4254-b6b8-ca392b43baf2
Urls:
[]
Language: en
---
# Sample: how the C# compiler rewrites an async method

`PageLoader.cs` contains a small async method that has everything the compiler needs to rewrite: a local that
lives across an `await`, two `await` expressions, a loop, and a `try`/`finally`.

Run it:

```
dotnet run -c Release
```

Look at the generated state machine:

```
dotnet tool install --global ilspycmd --version 10.1.0.8386
pwsh ./decompile.ps1
```

`decompile.ps1` builds both `Debug` and `Release` and writes the decompiled C# to `./decompiled/<configuration>.cs`
and the IL to `./decompiled/<configuration>.il`.

It decompiles with `-lv CSharp4` on purpose: `async`/`await` did not exist in C# 4, so ILSpy shows the state
machine instead of folding it back into the method it came from.
