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
 
返回序列中指定索引处的元素;如果索引超出范围,则返回默认值。
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。
适用于
ElementAtOrDefault<TSource>(IEnumerable<TSource>, Int32)
- 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。