Hooked on LINQ

Hooked on LINQ - Developers' Wiki
for .NET Language Integrated Query

Quick Search

Advanced Search »
{TOC}
Namespace:System.Linq
Assembly:System.Core.dll
Extends:IEnumerable<T>

Back to Standard Query Operator Index


Edit

Introduction

The ElementAtOrDefault operator returns the element at a given index in a sequence, or a default value if the index is out of range.

Edit

Method Signatures

public static TSource ElementAtOrDefault<TSource>(
    this IEnumerable<TSource> source, 
    int index)



Edit

Exceptions

Throws an ArgumentNullException if source is null.


Edit

Pseudo-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.

Edit

Loop 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.

Edit

Code Samples

TODO:Needs code sample.

If you would like to comment on this page, click on the Discuss button located on the top-right of each page. Feel free to edit any mistakes or omissions you find. If you have an objection or find in-appropriate content then contact the administrator. This website is not affiliated with Microsoft®, all content and opinions are those of the specific author and some advice, solutions and article may contain unintentional errors - please use care. Powered by ScrewTurn Wiki version 2.0.33. Some of the icons created by FamFamFam.