using System.Collections.Generic;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
public static class Program
public static async Task Main()
using CancellationTokenSource cts = new();
IAsyncEnumerable<int> sequence = Enumerable.Range(1, 10).ToAsyncEnumerable();
IAsyncEnumerator<int> enumerator = sequence.GetAsyncEnumerator(cts.Token);
bool moved = await enumerator.MoveNextAsync();
Console.WriteLine($"moved: {moved}");
#pragma warning disable 1998
public static async IAsyncEnumerable<T> ToAsyncEnumerable2<T>(
#pragma warning restore 1998
this IEnumerable<T> source,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
ArgumentNullException.ThrowIfNull(source);
cancellationToken.ThrowIfCancellationRequested();
using IEnumerator<T> enumerator = source.GetEnumerator();
cancellationToken.ThrowIfCancellationRequested();
if (!enumerator.MoveNext()) break;
yield return enumerator.Current;