1

I saw that everyone uses Task.WhenAll like in case 1. Can I just return Task which Task.WhenAll returns inside case 2?

Additional questions:

  1. Is case 2 will be executed sync or not?
  2. If i'll call MethodAsync() inside async method and await it, will I have 3 opened threads (main thread, MethodAsync thread, Task.WhenAll() thread). Will one thread wait next thread for completition?
  3. Which one case is better to use according to performance?
// case 1
public async Task MethodAsync()
{
    await Task.WhenAll(...);
}

// case 2
public Task MethodAsync()
{
    return Task.WhenAll(...);
}
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
Timur Vafin
  • 113
  • 1
  • 6
  • 1
    Also see [Difference between returning and awaiting a Task in an async method](https://stackoverflow.com/questions/24110213/difference-between-returning-and-awaiting-a-task-in-an-async-method) (and its duplicates) – gunr2171 Oct 14 '22 at 20:43
  • 4
    [eliding async await](https://blog.stephencleary.com/2016/12/eliding-async-await.html). – Guru Stron Oct 14 '22 at 20:43
  • `will I have 3 opened threads` - No; tasks aren't threads. – Stephen Cleary Oct 16 '22 at 00:34

0 Answers0