AsyncEnumerable.ElementAtOrDefaultAsync 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
ElementAtOrDefaultAsync<TSource>(IAsyncEnumerable<TSource>, Int32, CancellationToken) |
Returns the element at a specified index in a sequence, or a default value if the index is out of range. |
ElementAtOrDefaultAsync<TSource>(IAsyncEnumerable<TSource>, Index, CancellationToken) |
Returns the element at a specified index in a sequence or a default value if the index is out of range. |
ElementAtOrDefaultAsync<TSource>(IAsyncEnumerable<TSource>, Int32, CancellationToken)
- Source:
- ElementAtAsync.cs
Returns the element at a specified index in a sequence, or a default value if the index is out of range.
public static System.Threading.Tasks.ValueTask<TSource?> ElementAtOrDefaultAsync<TSource>(this System.Collections.Generic.IAsyncEnumerable<TSource> source, int index, System.Threading.CancellationToken cancellationToken = default);
static member ElementAtOrDefaultAsync : System.Collections.Generic.IAsyncEnumerable<'Source> * int * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<'Source>
<Extension()>
Public Function ElementAtOrDefaultAsync(Of TSource) (source As IAsyncEnumerable(Of TSource), index As Integer, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of TSource)
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
The default value of TSource
if index
is outside the bounds of the source sequence; otherwise, the
element at the specified position in the source sequence.
Exceptions
source
is null
.
Applies to
ElementAtOrDefaultAsync<TSource>(IAsyncEnumerable<TSource>, Index, CancellationToken)
- Source:
- ElementAtAsync.cs
Returns the element at a specified index in a sequence or a default value if the index is out of range.
public static System.Threading.Tasks.ValueTask<TSource?> ElementAtOrDefaultAsync<TSource>(this System.Collections.Generic.IAsyncEnumerable<TSource> source, Index index, System.Threading.CancellationToken cancellationToken = default);
static member ElementAtOrDefaultAsync : System.Collections.Generic.IAsyncEnumerable<'Source> * Index * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<'Source>
<Extension()>
Public Function ElementAtOrDefaultAsync(Of TSource) (source As IAsyncEnumerable(Of TSource), index As Index, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of TSource)
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
default
if index
is outside the bounds of the source
sequence; otherwise, the element at the specified position in the source
sequence.
Exceptions
source
is null
.
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.
The default value for reference and nullable types is null
.