Class IAsyncEnumerableExtensions
- Namespace
- Wolfgang.Extensions.IAsyncEnumerable
- Assembly
- Wolfgang.Extensions.IAsyncEnumerable.dll
A collection of extension methods for IAsyncEnumerable{T}.
public static class IAsyncEnumerableExtensions
- Inheritance
-
IAsyncEnumerableExtensions
- Inherited Members
Methods
ChunkAsync<T>(IAsyncEnumerable<T>, int, CancellationToken)
Splits an IAsyncEnumerable{T} into chunks of a specified maximum size.
public static IAsyncEnumerable<ICollection<T>> ChunkAsync<T>(this IAsyncEnumerable<T> source, int maxChunkSize, CancellationToken token = default)
Parameters
sourceIAsyncEnumerable<T>The source IAsyncEnumerable{T} to chunk.
maxChunkSizeintThe maximum size of each chunk.
tokenCancellationTokenA cancellation token to cancel the operation.
Returns
- IAsyncEnumerable<ICollection<T>>
An IAsyncEnumerable{ICollection{T}} representing the chunks.
Type Parameters
TThe type of elements in the IAsyncEnumerable{T}.
Exceptions
- ArgumentNullException
Thrown when
sourceis null.- ArgumentOutOfRangeException
Thrown when
maxChunkSizeis less than one.
DoAsync<T>(IAsyncEnumerable<T>, Action<T>, CancellationToken)
Executes a synchronous side-effect action on each element of an IAsyncEnumerable{T} without transforming the elements. The original items are yielded unchanged.
public static IAsyncEnumerable<T> DoAsync<T>(this IAsyncEnumerable<T> source, Action<T> action, CancellationToken token = default)
Parameters
sourceIAsyncEnumerable<T>The source IAsyncEnumerable{T}.
actionAction<T>The synchronous action to execute on each element.
tokenCancellationTokenA cancellation token to cancel the operation.
Returns
- IAsyncEnumerable<T>
An IAsyncEnumerable{T} that yields the original elements after executing the action.
Type Parameters
TThe type of elements in the IAsyncEnumerable{T}.
Examples
await foreach (var item in source.DoAsync(x => Console.WriteLine($"Processing: {x}")))
{
// item is unchanged
}
Remarks
Exceptions thrown by action propagate to the consuming
await foreach and terminate the enumeration. The cancellation token is
observed between elements (after each yield return), not while the action
is running — wrap long-running actions in their own cancellation if mid-action
cancellation is required.
Exceptions
- ArgumentNullException
Thrown when
sourceoractionis null.
DoAsync<T>(IAsyncEnumerable<T>, Func<T, Task>, CancellationToken)
Executes an asynchronous side-effect action on each element of an IAsyncEnumerable{T} without transforming the elements. The original items are yielded unchanged.
public static IAsyncEnumerable<T> DoAsync<T>(this IAsyncEnumerable<T> source, Func<T, Task> action, CancellationToken token = default)
Parameters
sourceIAsyncEnumerable<T>The source IAsyncEnumerable{T}.
actionFunc<T, Task>The asynchronous action to execute on each element.
tokenCancellationTokenA cancellation token to cancel the operation.
Returns
- IAsyncEnumerable<T>
An IAsyncEnumerable{T} that yields the original elements after executing the action.
Type Parameters
TThe type of elements in the IAsyncEnumerable{T}.
Examples
await foreach (var item in source.DoAsync(async x => await logger.LogAsync($"Processing: {x}")))
{
// item is unchanged
}
Exceptions
- ArgumentNullException
Thrown when
sourceoractionis null.
ForEachAsync<T>(IAsyncEnumerable<T>, Action<T>, CancellationToken)
Executes a synchronous action on each element of an IAsyncEnumerable{T}, consuming the sequence. This is a terminal operation.
public static Task ForEachAsync<T>(this IAsyncEnumerable<T> source, Action<T> action, CancellationToken token = default)
Parameters
sourceIAsyncEnumerable<T>The source IAsyncEnumerable{T}.
actionAction<T>The synchronous action to execute on each element.
tokenCancellationTokenA cancellation token to cancel the operation.
Returns
Type Parameters
TThe type of elements in the IAsyncEnumerable{T}.
Examples
await source.ForEachAsync(x => Console.WriteLine($"Processing: {x}"));
Exceptions
- ArgumentNullException
Thrown when
sourceoractionis null.
ForEachAsync<T>(IAsyncEnumerable<T>, Func<T, Task>, CancellationToken)
Executes an asynchronous action on each element of an IAsyncEnumerable{T}, consuming the sequence. This is a terminal operation.
public static Task ForEachAsync<T>(this IAsyncEnumerable<T> source, Func<T, Task> action, CancellationToken token = default)
Parameters
sourceIAsyncEnumerable<T>The source IAsyncEnumerable{T}.
actionFunc<T, Task>The asynchronous action to execute on each element.
tokenCancellationTokenA cancellation token to cancel the operation.
Returns
Type Parameters
TThe type of elements in the IAsyncEnumerable{T}.
Examples
await source.ForEachAsync(async x => await logger.LogAsync($"Processing: {x}"));
Exceptions
- ArgumentNullException
Thrown when
sourceoractionis null.
IsEmptyAsync<T>(IAsyncEnumerable<T>, CancellationToken)
Asynchronously determines whether a sequence contains no elements.
public static Task<bool> IsEmptyAsync<T>(this IAsyncEnumerable<T> source, CancellationToken token = default)
Parameters
sourceIAsyncEnumerable<T>The IAsyncEnumerable{T} to check.
tokenCancellationTokenA cancellation token to cancel the operation.
Returns
Type Parameters
TThe type of elements in the IAsyncEnumerable{T}.
Examples
if (await source.IsEmptyAsync())
{
Console.WriteLine("No items found.");
}
Exceptions
- ArgumentNullException
Thrown when
sourceis null.
IsNullOrEmptyAsync<T>(IAsyncEnumerable<T>?, CancellationToken)
Asynchronously determines whether a sequence is null or contains no elements.
public static Task<bool> IsNullOrEmptyAsync<T>(this IAsyncEnumerable<T>? source, CancellationToken token = default)
Parameters
sourceIAsyncEnumerable<T>The IAsyncEnumerable{T} to check. May be null.
tokenCancellationTokenA cancellation token to cancel the operation.
Returns
Type Parameters
TThe type of elements in the IAsyncEnumerable{T}.
Examples
if (await source.IsNullOrEmptyAsync())
{
Console.WriteLine("No items found.");
}
Remarks
Unlike IsEmptyAsync<T>(IAsyncEnumerable<T>, CancellationToken), this method is null-tolerant: passing a
null source returns true without throwing. The
cancellation token is observed only when the source is non-null (a null source
short-circuits before any token check).
NoneAsync<T>(IAsyncEnumerable<T>, Func<T, bool>, CancellationToken)
Asynchronously determines whether no element of a sequence satisfies a condition.
public static Task<bool> NoneAsync<T>(this IAsyncEnumerable<T> source, Func<T, bool> predicate, CancellationToken token = default)
Parameters
sourceIAsyncEnumerable<T>The IAsyncEnumerable{T} whose elements to apply the predicate to.
predicateFunc<T, bool>A function to test each element for a condition.
tokenCancellationTokenA cancellation token to cancel the operation.
Returns
- Task<bool>
true if no elements in the source sequence pass the test in the specified predicate; otherwise, false.
Type Parameters
TThe type of elements in the IAsyncEnumerable{T}.
Examples
if (await source.NoneAsync(x => x > 100))
{
Console.WriteLine("No items greater than 100.");
}
Exceptions
- ArgumentNullException
Thrown when
sourceorpredicateis null.
NoneAsync<T>(IAsyncEnumerable<T>, CancellationToken)
Asynchronously determines whether a sequence contains no elements.
public static Task<bool> NoneAsync<T>(this IAsyncEnumerable<T> source, CancellationToken token = default)
Parameters
sourceIAsyncEnumerable<T>The IAsyncEnumerable{T} to check.
tokenCancellationTokenA cancellation token to cancel the operation.
Returns
Type Parameters
TThe type of elements in the IAsyncEnumerable{T}.
Examples
if (await source.NoneAsync())
{
Console.WriteLine("No items found.");
}
Remarks
This overload is a naming alias for IsEmptyAsync<T>(IAsyncEnumerable<T>, CancellationToken) — the two
are observationally equivalent. Pick whichever reads more naturally at the
call site (e.g. await source.NoneAsync() as a guard, or
await source.IsEmptyAsync() as a state check).
Exceptions
- ArgumentNullException
Thrown when
sourceis null.