{TOC}
| Namespace: | System.Linq |
| Assembly: | System.Core.dll |
| Extends: | IEnumerable<T> |
Back to
Standard Query Operator IndexEditIntroduction
The ElementAt operator returns the element at a given index in a sequence. If you don't wan't an exception to be thrown when the index is out of range or less than zero, use the
ElementAtOrDefault operator instead.
EditMethod Signatures
public static TSource ElementAt<TSource>(
this IEnumerable<TSource> source,
int index)EditExceptions
Throws an ArgumentNullException if
source is null.
Throws an ArgumentOutOfRangeException if the index < 0 or if index falls beyond the end of the
source sequence.
EditPseudo-code
If
source is null, throw an ArgumentNullException.
If
index is < 0, throw an ArgumentOutOfRangeException.
If the
source sequence implements IList<T>, then
Return source[index]. (performance optomization)
Else Iterate the
source sequence.
Skip index times. Throw an ArgumentOutOfRangeException if there aren't enough elements.
Return the current element.
EditLoop Count
< 1. If the sequence is an IList, then it is index accessed which is optomized for each collection type. If the sequence is not an IList, the number of elements skipped is equal to the
index value.
EditCode Samples
TODO:Needs code sample.