Enumerable.ElementAtOrDefault<TSource>(IEnumerable<TSource>, Int32) Yöntem
Tanım
Dizin aralık dışında bir dizide belirtilen dizindeki öğeyi veya varsayılan değeri döndürür.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
Tür Parametreleri
- TSource
Öğelerinin türü source
.The type of the elements of source
.
Parametreler
- source
- IEnumerable<TSource>
' IEnumerable<T> Dan bir öğe döndürmek için.An IEnumerable<T> to return an element from.
- index
- Int32
Alınacak öğenin sıfır tabanlı dizini.The zero-based index of the element to retrieve.
Döndürülenler
- TSource
default
( TSource
) dizin, kaynak dizisinin sınırları dışındaysa; Aksi takdirde, kaynak dizide belirtilen konumdaki öğe.default
(TSource
) if the index is outside the bounds of the source sequence; otherwise, the element at the specified position in the source sequence.
Özel durumlar
source
, null
değeridir.source
is null
.
Örnekler
Aşağıdaki kod örneği, nasıl kullanılacağını göstermektedir ElementAtOrDefault .The following code example demonstrates how to use ElementAtOrDefault. Bu örnek, dizinin sınırları dışında bir dizin kullanır.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]
Açıklamalar
source
Uygulayan türü IList<T> , bu uygulama, belirtilen dizinde öğesini almak için kullanılır.If the type of source
implements IList<T>, that implementation is used to obtain the element at the specified index. Aksi takdirde, bu yöntem belirtilen öğeyi edinir.Otherwise, this method obtains the specified element.
Başvuru ve null yapılabilir türler için varsayılan değer null
.The default value for reference and nullable types is null
.