Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

Task.WhenAll: practical reference

Task.WhenAll: practical reference: Task.WhenAll completes after every task and aggregates results in input order.

Version: .NET API; verified against .NET 10 reference surface

Syntax

Task.WhenAll(/* overload-specific arguments */)

Task.WhenAll completes after every task and aggregates results in input order.

Use it for independent operations that should run concurrently.

One failure does not stop the others; the resulting Task becomes faulted only after all have completed.

An empty collection yields a completed Task; a null element causes ArgumentException.

The complete program actually uses Task.WhenAll; its exact expected stdout is 3.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

var results = await Task.WhenAll(Task.FromResult(1), Task.FromResult(2));
Console.WriteLine(results.Sum());

Result

3

The complete program actually uses `Task.WhenAll`; its exact expected stdout is `3`.

Sources