Closed
Description
Task.WhenAll(IEnumerable<Task> tasks)
tests for array and ICollection in order to appropriately size its target array.
public static Task WhenAll(IEnumerable<Task> tasks)
{
// Take a more efficient path if tasks is actually an array
Task[] taskArray = tasks as Task[];
if (taskArray != null)
{
return WhenAll(taskArray);
}
// Skip a List allocation/copy if tasks is a collection
ICollection<Task> taskCollection = tasks as ICollection<Task>;
if (taskCollection != null)
{
int index = 0;
taskArray = new Task[taskCollection.Count];
WhenAny does not, and this has showed up in customer profiles.
public static Task<Task> WhenAny(IEnumerable<Task> tasks)
{
if (tasks == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.tasks);
// Make a defensive copy, as the user may manipulate the tasks collection
// after we return but before the WhenAny asynchronously completes.
List<Task> taskList = new List<Task>();
foreach (Task task in tasks)
I don't know why WhenAll makes a copy iff it's not an array.