Async Lambda Statements
This is a quick post showing how to use Lambda statements with async code. It’s simple once you know how, but can take few mins if you haven’t seen an example.
There’s not a lot too it, you add the async
keyword and the return type if applicable to the left of the Lambda signature.
var add = async Task<int> (int a, int b) =>
{
await Task.Delay(1000);
return a + b;
};
var subtract = async Task (int a, int b) =>
{
await Task.Delay(1000);
Console.WriteLine($"a - b = {a - b}");
};
Console.WriteLine($"a + b = {await add(1, 2)}");
await subtract(1, 2);
That’s it, as I said, not a lot to it.