Пример #1
0
        /// <summary>
        /// Converts the <see cref="IAsyncEnumerable{T}" /> to a <see cref="List{T}" /> in an asynchronous operation.
        /// Validates that <paramref name="collection" /> is not null.
        /// </summary>
        /// <typeparam name="TSource">The type of the t source.</typeparam>
        /// <param name="collection">The list.</param>
        /// <param name="cancellationToken">The cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>List&lt;TSource&gt;.</returns>
        /// <remarks>Make sure to call .Dispose on Task,</remarks>
        public static async Task <List <TSource> > ToListAsync <TSource>([NotNull] this IAsyncEnumerable <TSource> collection, CancellationToken cancellationToken = default)
        {
            collection = collection.ArgumentNotNull();

            var returnList = new List <TSource>();

            await foreach (TSource element in collection.WithCancellation(cancellationToken))
            {
                returnList.Add(element);
            }

            return(returnList);
        }