Mega Code Archive
 
 
    
0397 ElementAt
Input: IEnumerable 
ElementAt picks the nth element from the sequence:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
 static void Main()
 {
 int[] numbers = { 1, 2, 3, 4, 5 };
 int third = numbers.ElementAt(2);
 int tenthError = numbers.ElementAt(9); // Exception 
 int tenth = numbers.ElementAtOrDefault(9); // 0
 Console.WriteLine(third);
 Console.WriteLine(tenth);
 }
}
The output:
Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range.
 Must be non-negative and less than the size of the collection.
Parameter name: index
 at System.ThrowHelper.ThrowArgumentOutOfRangeException()
 at System.SZArrayHelper.get_Item[T](Int32 index)
 at System.Linq.Enumerable.ElementAt[TSource](IEnumerable`1 source, Int32 index)
 at Program.Main() in c:\g\Program.cs:line 12