Читати англійською Редагувати

Поділитися через


AsyncEnumerable.ElementAtAsync Method

Definition

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Overloads

ElementAtAsync<TSource>(IAsyncEnumerable<TSource>, Index, CancellationToken)

Returns the element at a specified index in a sequence.

ElementAtAsync<TSource>(IAsyncEnumerable<TSource>, Int32, CancellationToken)

Returns the element at a specified index in a sequence.

ElementAtAsync<TSource>(IAsyncEnumerable<TSource>, Index, CancellationToken)

Source:
ElementAtAsync.cs

Returns the element at a specified index in a sequence.

C#
public static System.Threading.Tasks.ValueTask<TSource> ElementAtAsync<TSource>(this System.Collections.Generic.IAsyncEnumerable<TSource> source, Index index, System.Threading.CancellationToken cancellationToken = default);

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IAsyncEnumerable<TSource>

An IAsyncEnumerable<T> to return an element from.

index
Index

The index of the element to retrieve, which is either from the start or the end.

cancellationToken
CancellationToken

The token to monitor for cancellation requests. The default value is None.

Returns

ValueTask<TSource>

The element at the specified position in the source sequence.

Exceptions

source is null.

index is outside the bounds of the source sequence (via the returned task).

Remarks

If the type of source implements IList<T>, that implementation is used to obtain the element at the specified index. Otherwise, this method obtains the specified element.

This method throws an exception if index is out of range. To instead return a default value when the specified index is out of range, use the ElementAtOrDefaultAsync method.

Applies to

.NET 10 та інші версії
Продукт Версії
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided), 10
.NET Standard 2.0 (package-provided)

ElementAtAsync<TSource>(IAsyncEnumerable<TSource>, Int32, CancellationToken)

Source:
ElementAtAsync.cs

Returns the element at a specified index in a sequence.

C#
public static System.Threading.Tasks.ValueTask<TSource> ElementAtAsync<TSource>(this System.Collections.Generic.IAsyncEnumerable<TSource> source, int index, System.Threading.CancellationToken cancellationToken = default);

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IAsyncEnumerable<TSource>

An IAsyncEnumerable<T> to return an element from.

index
Int32

The index of the element to retrieve, which is either from the beginning or the end of the sequence.

cancellationToken
CancellationToken

The CancellationToken to monitor for cancellation requests. The default is None.

Returns

ValueTask<TSource>

The element at the specified position in the source sequence.

Exceptions

source is null.

index is outside the bounds of the source sequence (via the returned task).

Applies to

.NET 10 та інші версії
Продукт Версії
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided), 10
.NET Standard 2.0 (package-provided)