using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using System.Runtime.CompilerServices; internal static class Program { private static async System.Threading.Tasks.Task Main(string[] args) { await foreach (int count in CountsAsync(CancellationToken.None)) Console.WriteLine($"Count: {count}"); } internal static async IAsyncEnumerable CountsAsync([EnumeratorCancellation] CancellationToken token = default) { foreach (int count in new[] { 2, 4 }) { token.ThrowIfCancellationRequested(); await Task.Yield(); yield return count; } } }