user3552661
user3552661

Reputation: 539

Task.WhenAll result ordering

I understand from here that the task execution order for Task.WhenAll is not deterministic but I cannot find any information about result order.

Will the results collection contain the results in the order in which the tasks where ordered in the input or the results can be in any order?

From the tests that I did, it seems to keep the order but I need a confirmation.

Upvotes: 53

Views: 15336

Answers (1)

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149538

From MSDN:

Task.WhenAll<TResult>(IEnumerable<Task<TResult>>)

The overloads which take Task<TResult> as inputs contain this statement:

If none of the tasks faulted and none of the tasks were canceled, the resulting task will end in the RanToCompletion state. The Result 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 Result will return an TResult[] where arr[0] == t1.Result, arr[1] == t2.Result, and arr[2] == t3.Result).

Upvotes: 126

Related Questions