LINQ Interview Question #48
What does ElementAt do?
Filtering, Projection, Ordering & Element Operators Mid-Level Intermediate
Quick Interview Answer
ElementAt returns the element at a specified zero-based position. It throws ArgumentOutOfRangeException when the index is outside the sequence.
Detailed Explanation
ElementAt returns the element at a specified zero-based position. It throws ArgumentOutOfRangeException when the index is outside the sequence.
For indexable collections such as arrays or lists, direct indexing may be clearer. For general IEnumerable<T> sequences, ElementAt provides a common LINQ operation.
Code Example
var names = new[] { "Ali", "Sara", "John" };
string second = names.ElementAt(1);
// Sara