1

Given the following code:

var tasks1 = users.Select(async user => await Task.Delay(1000));
await Task.WhenAll(tasks1);

do I need to include the async / await syntax in this line of code?

I thought I can also write it like ...

var tasks2 = users.Select(user => Task.Delay(1000));
await Task.WhenAll(tasks2);

which does compile and run.

Is this something to do with error handling?

Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
  • Possible duplicate of [Any difference between “await Task.Run(); return;” and “return Task.Run()”?](https://stackoverflow.com/q/21033150/11683) – GSerg Dec 09 '19 at 00:16

1 Answers1

1

When you don't need a await , you could directly return the task (and skip marking the method as async)

There are some differences with the stacktrace when an exception is thrown.

Also small difference, when using dispose (or using), you probably need a await. Otherwise stuff could get disposed too soon.

do I need to include the async / await syntax in this line of code?

So probably not in this case :)

Julian
  • 33,915
  • 22
  • 119
  • 174