Could you please explain what is difference between using async methods inside Linq with/without async/await?
Example
private async Task<int> MultiplyAsync(int number, int multiplier)
{
await Task.Delay(100); // some kind of async operation
return number * multiplier;
}
public async Task DoWorkAsync()
{
int multiplier = 2;
int[] numberArray = new[] { 1, 2, 3, 4, 5 };
var tasksWithoutAsyncAwait = numberArray.Select(n => MultiplyAsync(n, multiplier));
var resultWithoutAsyncAwait = await Task.WhenAll(tasksWithoutAsyncAwait);
var tasksWithAsyncAwait = numberArray.Select(async n => await MultiplyAsync(n, multiplier));
var resultWithAsyncAwait = await Task.WhenAll(tasksWithAsyncAwait);
}
- What is difference between tasksWithoutAsyncAwait/resultWithoutAsyncAwait and tasksWithAsyncAwait/resultWithAsyncAwait?
- Which of these and in what cases should I use?
I tried to find an explanation for this, but without success. Sorry if this has already been discussed.