Input: | IEnumerable<TSource> |
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
java2s.com | Contact Us | Privacy Policy |
Copyright 2009 - 12 Demo Source and Support. All rights reserved. |
All other trademarks are property of their respective owners. |