title: To use Task.WhenAll, or not to use Task.WhenAll
body: <p>I am reviewing some code and trying to come up with a technical reason why you should or should not use <code>Task.WhenAll(Tasks[])</code> for essentially making Http calls in parallel.  The Http calls call a different microservice and I guess one of the calls may or may not take some time to execute... (I guess I am not really interested in that). I'm using BenchmarkDotNet to give me an idea of there is any more memory consumed, or if execution time is wildly different.  Here is an over-simplified example of the Benchmarks:</p>
  <pre><code>[Benchmark]
  public async Task&lt;string&gt; Task_WhenAll_Benchmark()
  {
    var t1 = Task1();
    var t2 = Task2();
  
    await Task.WhenAll(t1, t2);
  
    return $&quot;{t1.Result}===={t2.Result}&quot;;
  }
  
  [Benchmark]
  public async Task&lt;string&gt; Task_KeepItSimple_Benchmark()
  {
    return $&quot;{await Task1()}===={await Task2()}&quot;;
  }
  </code></pre>
  <p><code>Task1</code> and <code>Task2</code> methods are really simple (I have a static <code>HttpClient</code> in the class)</p>
  <pre><code>public async Task&lt;string&gt; Task1()
  {
    using (var request = await httpClient.GetAsync(&quot;http://localhost:8000/1.txt&quot;))
    {
      return $&quot;task{await request.Content.ReadAsStringAsync()}&quot;;
    }
  }
  
  public async Task&lt;string&gt; Task2()
  {
    using (var request = await httpClient.GetAsync(&quot;http://localhost:8000/2.txt&quot;))
    {
      return $&quot;task{await request.Content.ReadAsStringAsync()}&quot;;
    }
  }
  </code></pre>
  <p>And my results</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Method</th>
  <th style="text-align: right;">Mean</th>
  <th style="text-align: right;">Error</th>
  <th style="text-align: right;">StdDev</th>
  <th style="text-align: right;">Gen 0</th>
  <th style="text-align: right;">Gen 1</th>
  <th style="text-align: right;">Gen 2</th>
  <th style="text-align: right;">Allocated</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>Task_WhenAll_Benchmark</td>
  <td style="text-align: right;">1.138 ms</td>
  <td style="text-align: right;">0.0561 ms</td>
  <td style="text-align: right;">0.1601 ms</td>
  <td style="text-align: right;">-</td>
  <td style="text-align: right;">-</td>
  <td style="text-align: right;">-</td>
  <td style="text-align: right;">64 KB</td>
  </tr>
  <tr>
  <td>Task_KeepItSimple_Benchmark</td>
  <td style="text-align: right;">1.461 ms</td>
  <td style="text-align: right;">0.0822 ms</td>
  <td style="text-align: right;">0.2331 ms</td>
  <td style="text-align: right;">-</td>
  <td style="text-align: right;">-</td>
  <td style="text-align: right;">-</td>
  <td style="text-align: right;">64 KB</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>As you can see, memory not an issue and there isn't a great deal of time in the execution either.</p>
  <p>My question really is, is there a technical reason why you should or not use Task.WhenAll()? Is it just a preference?</p>
  <p>I came across <a href="https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AsyncGuidance.md" rel="nofollow noreferrer">Async Guidance</a> from a guy on the .net core team but it hasn't really covered this scenario.</p>
  <p>Edit: this is .net framework (4.6.1) rather than core!</p>
  <p>Edit 2: Update one of the benchmarks as suggested in a comment below.</p>
  <p>I updated the benchmark for the KeepItSimple approach...</p>
  <pre><code>[Benchmark]
  public async Task&lt;string&gt; Task_KeepItSimple_Benchmark()
  {
    var t1 = Task1();
    var t2 = Task2();
  
    return $&quot;{await t1}===={await t2}&quot;;
  }
  </code></pre>
  <p>and have the following results:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Method</th>
  <th style="text-align: right;">Mean</th>
  <th style="text-align: right;">Error</th>
  <th style="text-align: right;">StdDev</th>
  <th style="text-align: right;">Gen 0</th>
  <th style="text-align: right;">Gen 1</th>
  <th style="text-align: right;">Gen 2</th>
  <th style="text-align: right;">Allocated</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>Task_WhenAll_Benchmark</td>
  <td style="text-align: right;">1.134 ms</td>
  <td style="text-align: right;">0.0566 ms</td>
  <td style="text-align: right;">0.1613 ms</td>
  <td style="text-align: right;">-</td>
  <td style="text-align: right;">-</td>
  <td style="text-align: right;">-</td>
  <td style="text-align: right;">64 KB</td>
  </tr>
  <tr>
  <td>Task_KeepItSimple_Benchmark</td>
  <td style="text-align: right;">1.081 ms</td>
  <td style="text-align: right;">0.0377 ms</td>
  <td style="text-align: right;">0.1070 ms</td>
  <td style="text-align: right;">-</td>
  <td style="text-align: right;">-</td>
  <td style="text-align: right;">-</td>
  <td style="text-align: right;">64 KB</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>And now I am even more confused - how is the execution faster (albeit a tiny amount!)? I thought execution of the code started when you <code>await</code> the result...</p>