Enumerable.ElementAtOrDefault Method
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.
ElementAtOrDefault<TSource>(IEnumerable<TSource>, Index) |
Returns the element at a specified index in a sequence or a default value if the index is out of range. |
ElementAtOrDefault<TSource>(IEnumerable<TSource>, Int32) |
Returns the element at a specified index in a sequence or a default value if the index is out of range. |
- Source:
- ElementAt.cs
- Source:
- ElementAt.cs
- Source:
- ElementAt.cs
Returns the element at a specified index in a sequence or a default value if the index is out of range.
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static TSource ElementAtOrDefault(System::Collections::Generic::IEnumerable<TSource> ^ source, Index index);
public static TSource? ElementAtOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Index index);
static member ElementAtOrDefault : seq<'Source> * Index -> 'Source
<Extension()>
Public Function ElementAtOrDefault(Of TSource) (source As IEnumerable(Of TSource), index As Index) As TSource
Type Parameters
- TSource
The type of the elements of source
.
Parameters
- source
- IEnumerable<TSource>
An IEnumerable<T> to return an element from.
- index
- Index
The index of the element to retrieve, which is either from the beginning or the end of the sequence.
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
.
Applies to
.NET 9 та інші версії
Продукт | Версії |
---|---|
.NET | 6, 7, 8, 9 |
- Source:
- ElementAt.cs
- Source:
- ElementAt.cs
- Source:
- ElementAt.cs
Returns the element at a specified index in a sequence or a default value if the index is out of range.
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static TSource ElementAtOrDefault(System::Collections::Generic::IEnumerable<TSource> ^ source, int index);
public static TSource ElementAtOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, int index);
public static TSource? ElementAtOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, int index);
static member ElementAtOrDefault : seq<'Source> * int -> 'Source
<Extension()>
Public Function ElementAtOrDefault(Of TSource) (source As IEnumerable(Of TSource), index As Integer) As TSource
Type Parameters
- TSource
The type of the elements of source
.
Parameters
- source
- IEnumerable<TSource>
An IEnumerable<T> to return an element from.
- index
- Int32
The zero-based index of the element to retrieve.
Returns
default
(TSource
) if the index is outside the bounds of the source sequence; otherwise, the element at the specified position in the source sequence.
Exceptions
source
is null
.
Examples
The following code example demonstrates how to use ElementAtOrDefault. This example uses an index that is outside the bounds of the array.
string[] names =
{ "Hartono, Tommy", "Adams, Terry", "Andersen, Henriette Thaulow",
"Hedlund, Magnus", "Ito, Shu" };
int index = 20;
string name = names.ElementAtOrDefault(index);
Console.WriteLine(
"The name chosen at index {0} is '{1}'.",
index,
String.IsNullOrEmpty(name) ? "<no name at this index>" : name);
/*
This code produces the following output:
The name chosen at index 20 is '<no name at this index>'.
*/
' Create an array of strings.
Dim names() As String =
{"Hartono, Tommy", "Adams, Terry", "Andersen, Henriette Thaulow", "Hedlund, Magnus", "Ito, Shu"}
Dim index As Integer = 20
' Get a string at an index that is out of range in the array.
Dim name As String = names.ElementAtOrDefault(index)
Dim text As String = If(String.IsNullOrEmpty(name), "[THERE IS NO NAME AT THIS INDEX]", name)
' Display the output.
Console.WriteLine($"The name chosen at index {index} is {text}")
' This code produces the following output:
'
' The name chosen at index 20 is [THERE IS NO NAME AT THIS INDEX]
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
.
Applies to
.NET 9 та інші версії
Продукт | Версії |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
Відгук про .NET
.NET – це проект із відкритим кодом. Виберіть посилання, щоб надати відгук: