title: Has my collegue reproduced the exact behavior of Task.WhenAll<TResult>?
body: <p>When reviewing work by a contractor, I came upon the following extension method:</p>
  <pre class="lang-cs prettyprint-override"><code>public static class TaskExtensions
  {
      public static async Task&lt;IEnumerable&lt;TResult&gt;&gt; WhenAllSynchronousExecution&lt;TResult&gt;(this IEnumerable&lt;Task&lt;TResult&gt;&gt; tasks, CancellationToken token = default(CancellationToken))
      {
          var results = new List&lt;TResult&gt;();
          var exceptions = new List&lt;Exception&gt;();
          foreach (var task in tasks)
          {
              if (task == null)
              {
                  throw new ArgumentNullException(nameof(tasks));
              }
              
              if (token.IsCancellationRequested) {
                  exceptions.Add(new OperationCanceledException(&quot;Tasks collection cancelled&quot;, token));
                  break;
              }
  
              try
              {
                  results.Add(await task);
              }
              catch (Exception ex)
              {
                  exceptions.Add(ex);
              }
          }
          
          if (exceptions.Any()) {
              throw new AggregateException(exceptions);
          }
          
          return results;
      }
  }
  </code></pre>
  <p>To this looks to be exactly what <a href="https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.whenall?view=net-6.0#system-threading-tasks-task-whenall-1(system-collections-generic-ienumerable((system-threading-tasks-task((-0)))))" rel="nofollow noreferrer"><code>Task.WhenAll&lt;TResult&gt;</code></a> already does. quote the remarks:</p>
  <blockquote>
  <p>The call to WhenAll(IEnumerable&lt;Task&gt;) method does not block the calling thread. However, a call to the returned Result property does block the calling thread.</p>
  <p>If any of the supplied tasks completes in a faulted state, the returned task will also complete in a Faulted state, where its exceptions will contain the aggregation of the set of unwrapped exceptions from each of the supplied tasks.</p>
  <p>If none of the supplied tasks faulted but at least one of them was canceled, the returned task will end in the Canceled state.</p>
  <p>If none of the tasks faulted and none of the tasks were canceled, the resulting task will end in the RanToCompletion state. The Task.Result property of the returned task will be set to an array containing all of the results of the supplied tasks in the same order as they were provided (e.g. if the input tasks array contained t1, t2, t3, the output task's Task.Result property will return an TResult[] where arr[0] == t1.Result, arr[1] == t2.Result, and arr[2] == t3.Result).</p>
  <p>If the tasks argument contains no tasks, the returned task will immediately transition to a RanToCompletion state before it's returned to the caller. The returned TResult[] will be an array of 0 elements.</p>
  </blockquote>
  <p>So to me it seems they've recreated an existing method. The only difference seems to be the checking of the <code>CancellationToken</code>, but I don't see mych added value there (it's not used in the code). And maybe that it's an extension method, but that's also not used in the code (it's called like <code>var task = TaskExtensions.WhenAllSynchronousExecution(tasks);</code>)</p>
  <p>But this colleague is quite &quot;sensitive&quot;. So I need to pick my battles. So is this just a custom,meagre recreation of <code>Task.WhenAll&lt;TResult&gt;</code>?</p>