Show / Hide Table of Contents

Class AsyncEnumerable

Provides extension methods for IAsyncEnumerable<T> interface.

Inheritance
object
AsyncEnumerable
Inherited Members
object.GetType()
object.MemberwiseClone()
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: DotNext.Collections.Generic
Assembly: DotNext.dll
Syntax
public static class AsyncEnumerable

Methods

View Source

CopyAsync<T>(IAsyncEnumerable<T>, int, MemoryAllocator<T>?, CancellationToken)

Creates a copy of the elements from the collection.

Declaration
public static Task<MemoryOwner<T>> CopyAsync<T>(this IAsyncEnumerable<T> enumerable, int sizeHint = 0, MemoryAllocator<T>? allocator = null, CancellationToken token = default)
Parameters
Type Name Description
IAsyncEnumerable<T> enumerable

The collection to copy.

int sizeHint

The approximate size of the collection, if known.

MemoryAllocator<T> allocator

The allocator of the memory block used to place copied elements.

CancellationToken token

The token that can be used to cancel the enumeration.

Returns
Type Description
Task<MemoryOwner<T>>

Rented memory block containing a copy of the elements from the collection.

Type Parameters
Name Description
T

The type of elements in the collection.

Exceptions
Type Condition
ArgumentNullException

enumerable is null.

ArgumentOutOfRangeException

sizeHint is less than zero.

OperationCanceledException

The operation has been canceled.

View Source

FirstOrNoneAsync<T>(IAsyncEnumerable<T>, Predicate<T>, CancellationToken)

Returns the first element in a sequence that satisfies a specified condition.

Declaration
public static ValueTask<Optional<T>> FirstOrNoneAsync<T>(this IAsyncEnumerable<T> collection, Predicate<T> filter, CancellationToken token = default)
Parameters
Type Name Description
IAsyncEnumerable<T> collection
Predicate<T> filter

A function to test each element for a condition.

CancellationToken token

The token that can be used to cancel enumeration.

Returns
Type Description
ValueTask<Optional<T>>

The first element in the sequence that matches to the specified filter; or empty value.

Type Parameters
Name Description
T
Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

View Source

FirstOrNoneAsync<T>(IAsyncEnumerable<T>, CancellationToken)

Obtains the first element of a sequence; or None if the sequence is empty.

Declaration
public static ValueTask<Optional<T>> FirstOrNoneAsync<T>(this IAsyncEnumerable<T> collection, CancellationToken token = default)
Parameters
Type Name Description
IAsyncEnumerable<T> collection
CancellationToken token

The token that can be used to cancel enumeration.

Returns
Type Description
ValueTask<Optional<T>>

The first element in the sequence; or None if sequence is empty.

Type Parameters
Name Description
T
Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

View Source

FirstOrNullAsync<T>(IAsyncEnumerable<T>, CancellationToken)

Obtains the first value of a sequence; or null if the sequence is empty.

Declaration
public static ValueTask<T?> FirstOrNullAsync<T>(this IAsyncEnumerable<T> seq, CancellationToken token = default) where T : struct
Parameters
Type Name Description
IAsyncEnumerable<T> seq
CancellationToken token

The token that can be used to cancel enumeration.

Returns
Type Description
ValueTask<T?>

First element in the sequence; or null if sequence is empty.

Type Parameters
Name Description
T
Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

View Source

ForEachAsync<T>(IAsyncEnumerable<T>, Action<T>, CancellationToken)

Applies specified action to each element of the collection asynchronously.

Declaration
public static ValueTask ForEachAsync<T>(this IAsyncEnumerable<T> collection, Action<T> action, CancellationToken token = default) where T : allows ref struct
Parameters
Type Name Description
IAsyncEnumerable<T> collection
Action<T> action

An action to applied for each element.

CancellationToken token

The token that can be used to cancel the enumeration.

Returns
Type Description
ValueTask

The task representing asynchronous execution of this method.

Type Parameters
Name Description
T
Exceptions
Type Condition
OperationCanceledException

The enumeration has been canceled.

View Source

ForEachAsync<T>(IAsyncEnumerable<T>, Func<T, CancellationToken, ValueTask>, CancellationToken)

Applies the specified action to each element of the collection asynchronously.

Declaration
public static ValueTask ForEachAsync<T>(this IAsyncEnumerable<T> collection, Func<T, CancellationToken, ValueTask> action, CancellationToken token = default) where T : allows ref struct
Parameters
Type Name Description
IAsyncEnumerable<T> collection
Func<T, CancellationToken, ValueTask> action

An action to applied for each element.

CancellationToken token

The token that can be used to cancel the enumeration.

Returns
Type Description
ValueTask

The task representing asynchronous execution of this method.

Type Parameters
Name Description
T
Exceptions
Type Condition
OperationCanceledException

The enumeration has been canceled.

View Source

LastOrNoneAsync<T>(IAsyncEnumerable<T>, CancellationToken)

Obtains the last element of a sequence; or None if the sequence is empty.

Declaration
public static ValueTask<Optional<T>> LastOrNoneAsync<T>(this IAsyncEnumerable<T> collection, CancellationToken token = default)
Parameters
Type Name Description
IAsyncEnumerable<T> collection
CancellationToken token

The token that can be used to cancel enumeration.

Returns
Type Description
ValueTask<Optional<T>>

The last element in the sequence; or None if sequence is empty.

Type Parameters
Name Description
T
Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

View Source

LastOrNullAsync<T>(IAsyncEnumerable<T>, CancellationToken)

Obtains the last value of a sequence; or null if the sequence is empty.

Declaration
public static ValueTask<T?> LastOrNullAsync<T>(this IAsyncEnumerable<T> seq, CancellationToken token = default) where T : struct
Parameters
Type Name Description
IAsyncEnumerable<T> seq
CancellationToken token

The token that can be used to cancel enumeration.

Returns
Type Description
ValueTask<T?>

The last element in the sequence; or null if sequence is empty.

Type Parameters
Name Description
T
Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

View Source

Singleton<T>(T)

Constructs read-only sequence with a single item in it.

Declaration
public static IAsyncEnumerable<T> Singleton<T>(T item)
Parameters
Type Name Description
T item

An item to be placed into list.

Returns
Type Description
IAsyncEnumerable<T>

Read-only list containing single item.

Type Parameters
Name Description
T
View Source

SkipNulls<T>(IAsyncEnumerable<T?>)

Skip null values in the collection.

Declaration
public static IAsyncEnumerable<T> SkipNulls<T>(this IAsyncEnumerable<T?> collection) where T : class
Parameters
Type Name Description
IAsyncEnumerable<T> collection

A collection to check. Cannot be null.

Returns
Type Description
IAsyncEnumerable<T>

Modified lazy collection without null values.

Type Parameters
Name Description
T

Type of elements in the collection.

View Source

Throw<T>(Exception)

Gets an asynchronous collection that throws the specified exception.

Declaration
public static IAsyncEnumerable<T> Throw<T>(Exception e) where T : notnull, allows ref struct
Parameters
Type Name Description
Exception e

The exception to be thrown by the enumerator.

Returns
Type Description
IAsyncEnumerable<T>

Empty asynchronous collection which enumerator throws e.

Type Parameters
Name Description
T
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾