2

Let's take a very simple example. What is the difference between this:

var sublist = Task.WhenAll(list.Select(x => x.getYAsync()));

and this:

var sublist = Task.WhenAll(list.Select(async x => await x.getYAsync()));

In both cases, the type of sublist is the same, so the only difference is semantic. Does one perform better than the other? Is one more standard than the other?

gzak
  • 3,908
  • 6
  • 33
  • 56

1 Answers1

3

The only significant difference regards exception propagation.

If getYAsync throws an exception which isn't stored in the returned task* the first option would have an exception when calling Task.WhenAll while the second option would have the exception stored in the task returned from Task.WhenAll which would be thrown when that task is awaited.

Other than that there's the state machine being built in the async lambda which has some (very small) overhead.

The first option performs slightly better, while the second is more standard than the other. The safer option is to explicitly state the async and await but in such a simple case its somewhat redundant.

* For example: Task getYAsync() { throw new Exception(); return Task.Delay(1)}

i3arnon
  • 113,022
  • 33
  • 324
  • 344