Show / Hide Table of Contents

Class AsyncEnumerable

Provides extension methods for IAsyncEnumerable<T> interface.

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

Methods

| Edit this page 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.

| Edit this page View Source

ElementAtAsync<T>(IAsyncEnumerable<T>, int, CancellationToken)

Obtains element at the specified index in the sequence.

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

Source collection.

int index

Index of the element to read.

CancellationToken token

The token that can be used to cancel enumeration.

Returns
Type Description
ValueTask<Optional<T>>

The requested element; or None if index is out of range.

Type Parameters
Name Description
T

Type of elements in the sequence.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

| Edit this page View Source

Empty<T>()

Gets an empty asynchronous collection.

Declaration
public static IAsyncEnumerable<T> Empty<T>()
Returns
Type Description
IAsyncEnumerable<T>

Empty asynchronous collection.

Type Parameters
Name Description
T

The type of the elements in the collection.

| Edit this page 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> seq, Predicate<T> filter, CancellationToken token = default)
Parameters
Type Name Description
IAsyncEnumerable<T> seq

A collection to return an element from.

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

The type of the elements of source.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

| Edit this page 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> seq, CancellationToken token = default)
Parameters
Type Name Description
IAsyncEnumerable<T> seq

A sequence to check. Cannot be null.

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

Type of elements in the sequence.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

| Edit this page 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

A sequence to check. Cannot be null.

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

Type of elements in the sequence.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

| Edit this page 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)
Parameters
Type Name Description
IAsyncEnumerable<T> collection

A collection to enumerate. Cannot be null.

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

Type of elements in the collection.

Exceptions
Type Condition
OperationCanceledException

The enumeration has been canceled.

| Edit this page 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)
Parameters
Type Name Description
IAsyncEnumerable<T> collection

A collection to enumerate. Cannot be null.

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

Type of elements in the collection.

Exceptions
Type Condition
OperationCanceledException

The enumeration has been canceled.

| Edit this page 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> seq, CancellationToken token = default)
Parameters
Type Name Description
IAsyncEnumerable<T> seq

A sequence to check. Cannot be null.

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

Type of elements in the sequence.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

| Edit this page 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

A sequence to check. Cannot be null.

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

Type of elements in the sequence.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

| Edit this page 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

Type of list items.

| Edit this page View Source

SkipAsync<T>(IAsyncEnumerator<T>, int)

Bypasses a specified number of elements in a sequence.

Declaration
public static ValueTask<bool> SkipAsync<T>(this IAsyncEnumerator<T> enumerator, int count)
Parameters
Type Name Description
IAsyncEnumerator<T> enumerator

Enumerator to modify. Cannot be null.

int count

The number of elements to skip.

Returns
Type Description
ValueTask<bool>

true, if current element is available; otherwise, false.

Type Parameters
Name Description
T

The type of the elements in the sequence.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

| Edit this page 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.

| Edit this page View Source

Throw<T>(Exception)

Gets an asynchronous collection that throws the specified exception.

Declaration
public static IAsyncEnumerable<T> Throw<T>(Exception e)
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

The type of the elements in the collection.

| Edit this page View Source

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

Converts asynchronous collection to the array.

Declaration
public static Task<T[]> ToArrayAsync<T>(this IAsyncEnumerable<T> collection, int initialCapacity = 10, MemoryAllocator<T>? allocator = null, CancellationToken token = default)
Parameters
Type Name Description
IAsyncEnumerable<T> collection

The asynchronous collection.

int initialCapacity

The initial capacity of internal buffer.

MemoryAllocator<T> allocator

The memory allocator used by internal buffer.

CancellationToken token

The token that can be used to cancel the operation.

Returns
Type Description
Task<T[]>

The array representing all elements from the source collection.

Type Parameters
Name Description
T

Type of elements in the collection.

Exceptions
Type Condition
OperationCanceledException

The operation has been canceled.

  • Edit this page
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾