123456789101112131415161718192021222324252627282930313233343536 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT License.
- // See the LICENSE file in the project root for more information.
- using System.Collections.Generic;
- using System.Threading;
- using System.Threading.Tasks;
- namespace System.Linq
- {
- public static partial class AsyncEnumerableEx
- {
- /// <summary>
- /// Determines whether an async-enumerable sequence is empty.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
- /// <param name="source">An async-enumerable sequence to check for emptiness.</param>
- /// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
- /// <returns>An async-enumerable sequence containing a single element determining whether the source sequence is empty.</returns>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
- public static ValueTask<bool> IsEmptyAsync<TSource>(this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- return Core(source, cancellationToken);
- static async ValueTask<bool> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
- {
- await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
- return !await e.MoveNextAsync();
- }
- }
- }
- }
|