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

source IAsyncEnumerable<T>

The source IAsyncEnumerable{T} to chunk.

maxChunkSize int

The maximum size of each chunk.

token CancellationToken

A cancellation token to cancel the operation.

Returns

IAsyncEnumerable<ICollection<T>>

An IAsyncEnumerable{ICollection{T}} representing the chunks.

Type Parameters

T

The type of elements in the IAsyncEnumerable{T}.

Exceptions

ArgumentNullException

Thrown when source is null.

ArgumentOutOfRangeException

Thrown when maxChunkSize is 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

source IAsyncEnumerable<T>

The source IAsyncEnumerable{T}.

action Action<T>

The synchronous action to execute on each element.

token CancellationToken

A cancellation token to cancel the operation.

Returns

IAsyncEnumerable<T>

An IAsyncEnumerable{T} that yields the original elements after executing the action.

Type Parameters

T

The 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 source or action is 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

source IAsyncEnumerable<T>

The source IAsyncEnumerable{T}.

action Func<T, Task>

The asynchronous action to execute on each element.

token CancellationToken

A cancellation token to cancel the operation.

Returns

IAsyncEnumerable<T>

An IAsyncEnumerable{T} that yields the original elements after executing the action.

Type Parameters

T

The 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 source or action is 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

source IAsyncEnumerable<T>

The source IAsyncEnumerable{T}.

action Action<T>

The synchronous action to execute on each element.

token CancellationToken

A cancellation token to cancel the operation.

Returns

Task

Type Parameters

T

The type of elements in the IAsyncEnumerable{T}.

Examples

await source.ForEachAsync(x => Console.WriteLine($"Processing: {x}"));

Exceptions

ArgumentNullException

Thrown when source or action is 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

source IAsyncEnumerable<T>

The source IAsyncEnumerable{T}.

action Func<T, Task>

The asynchronous action to execute on each element.

token CancellationToken

A cancellation token to cancel the operation.

Returns

Task

Type Parameters

T

The type of elements in the IAsyncEnumerable{T}.

Examples

await source.ForEachAsync(async x => await logger.LogAsync($"Processing: {x}"));

Exceptions

ArgumentNullException

Thrown when source or action is 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

source IAsyncEnumerable<T>

The IAsyncEnumerable{T} to check.

token CancellationToken

A cancellation token to cancel the operation.

Returns

Task<bool>

true if the source sequence contains no elements; otherwise, false.

Type Parameters

T

The type of elements in the IAsyncEnumerable{T}.

Examples

if (await source.IsEmptyAsync())
{
    Console.WriteLine("No items found.");
}

Exceptions

ArgumentNullException

Thrown when source is 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

source IAsyncEnumerable<T>

The IAsyncEnumerable{T} to check. May be null.

token CancellationToken

A cancellation token to cancel the operation.

Returns

Task<bool>

true if the source sequence is null or contains no elements; otherwise, false.

Type Parameters

T

The 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

source IAsyncEnumerable<T>

The IAsyncEnumerable{T} whose elements to apply the predicate to.

predicate Func<T, bool>

A function to test each element for a condition.

token CancellationToken

A 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

T

The 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 source or predicate is 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

source IAsyncEnumerable<T>

The IAsyncEnumerable{T} to check.

token CancellationToken

A cancellation token to cancel the operation.

Returns

Task<bool>

true if the source sequence contains no elements; otherwise, false.

Type Parameters

T

The 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 source is null.