Tip #019
await at the top level
About this tip
Top-level statements await directly. No async Task Main. It is available since C# 9. The key line is var total = await SumAsync([1, 2, 3]) in async-main.cs. Run it and the output reads Total: 6. It fits in 8 lines; run it with dotnet run async-main.cs.
Did this tip save you some time? Consider sponsoring me on GitHub to fuel the next one.
Sponsor on GitHub
Railway
Referral
Need somewhere to deploy?
My link gets you $20 in credits.
Claim $20 creditI may earn a commission if you become a customer.
Code
var total = await SumAsync([1, 2, 3]);Console.WriteLine($"Total: {total}");
static async Task<int> SumAsync(int[] xs){ await Task.Delay(10); return xs.Sum();}Output
$ dotnet run async-main.csTotal: 6