/// </exception>
public static Task WhenAll(IEnumerable<Task> tasks)
{
- // Take a more efficient path if tasks is actually an array
- if (tasks is Task[] taskArray)
- {
- return WhenAll(taskArray);
- }
-
// Skip a List allocation/copy if tasks is a collection
if (tasks is ICollection<Task> taskCollection)
{
+ // Take a more efficient path if tasks is actually an array
+ if (tasks is Task[] taskArray)
+ {
+ return WhenAll(taskArray);
+ }
+
int index = 0;
taskArray = new Task[taskCollection.Count];
foreach (Task task in tasks)
/// </exception>
public static Task<Task> WhenAny(IEnumerable<Task> tasks)
{
+ // Skip a List allocation/copy if tasks is a collection
+ if (tasks is ICollection<Task> taskCollection)
+ {
+ // Take a more efficient path if tasks is actually an array
+ if (tasks is Task[] taskArray)
+ {
+ return WhenAny(taskArray);
+ }
+
+ int index = 0;
+ taskArray = new Task[taskCollection.Count];
+ foreach (Task task in tasks)
+ {
+ if (task == null) ThrowHelper.ThrowArgumentException(ExceptionResource.Task_MultiTaskContinuation_NullTask, ExceptionArgument.tasks);
+ taskArray[index++] = task;
+ }
+ return TaskFactory.CommonCWAnyLogic(taskArray);
+ }
+
if (tasks == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.tasks);
// Make a defensive copy, as the user may manipulate the tasks collection