{TOC}
| Namespace: | System.Linq |
| Assembly: | System.Core.dll |
| Extends: | IEnumerable<T> |
Back to
Standard Query Operator IndexEditIntroduction
The ElementAtOrDefault operator returns the element at a given index in a sequence, or a default value if the index is out of range.
EditMethod Signatures
public static TSource ElementAtOrDefault<TSource>(
this IEnumerable<TSource> source,
int index)EditExceptions
Throws an ArgumentNullException if
source is null.
EditPseudo-code
If
source is null, throw an ArgumentNullException.
If
index < 0 return
default(TSource);
If the
source sequence implements IList<T>
If index >= source.Count then return default(TSource);
Return source[index]. (performance optomization)
Else Iterate the
source sequence.
Skip index times. Return default(TSource); 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.