LINQ ElementAt Query Operator

By Peter Bromberg

LINQ provides two extension methods for retreiving the element at a given index in a List or other IEnumerable object. ElementAt returns the element at a given index.

// Create a new generic list of ints
<int> l = new List<int>();

l.Add(1); // Add 1 to the list
l.Add(5); // Add 5 to the list
l.Add(3); // Add 3 to the list

// Returns 1 as 1 exists at index 0
int value = l.ElementAt(0);

// Returns 3 as 3 exists at index 2
value = l.ElementAt(2);

LINQ ElementAt Query Operator  (1059 Views)