Enumerable.ElementAtOrDefault 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
| 名称 | 说明 |
|---|---|
| ElementAtOrDefault<TSource>(IEnumerable<TSource>, Index) |
返回序列中指定索引处的元素;如果索引范围不足,则返回默认值。 |
| ElementAtOrDefault<TSource>(IEnumerable<TSource>, Int32) |
返回序列中指定索引处的元素;如果索引范围不足,则返回默认值。 |
ElementAtOrDefault<TSource>(IEnumerable<TSource>, Index)
- Source:
- ElementAt.cs
- Source:
- ElementAt.cs
- Source:
- ElementAt.cs
- Source:
- ElementAt.cs
- Source:
- ElementAt.cs
返回序列中指定索引处的元素;如果索引范围不足,则返回默认值。
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
类型参数
- TSource
的元素 source的类型。
参数
- source
- IEnumerable<TSource>
要从中返回元素的一 IEnumerable<T> 个。
- index
- Index
要检索的元素的索引,该索引是从序列的开头或结尾。
返回
default 如果 index 超出序列的 source 边界,则为序列的边界;否则为序列中指定位置的 source 元素。
例外
source 是 null。
注解
如果实现的类型 source ,则使用该实现 IList<T>获取指定索引处的元素。 否则,此方法将获取指定的元素。
引用和可为 null 类型的默认值为 null。
适用于
ElementAtOrDefault<TSource>(IEnumerable<TSource>, Int32)
- Source:
- ElementAt.cs
- Source:
- ElementAt.cs
- Source:
- ElementAt.cs
- Source:
- ElementAt.cs
- Source:
- ElementAt.cs
返回序列中指定索引处的元素;如果索引范围不足,则返回默认值。
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
类型参数
- TSource
的元素 source的类型。
参数
- source
- IEnumerable<TSource>
要从中返回元素的一 IEnumerable<T> 个。
- index
- Int32
要检索的元素的从零开始的索引。
返回
default(TSource) 如果索引超出源序列的边界,则为元素;否则,该元素位于源序列中的指定位置。
例外
source 是 null。
示例
下面的代码示例演示如何使用 ElementAtOrDefault。 此示例使用位于数组边界之外的索引。
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]
注解
如果实现的类型 source ,则使用该实现 IList<T>获取指定索引处的元素。 否则,此方法将获取指定的元素。
引用和可为 null 类型的默认值为 null。