source
IEnumerable<TSource>
to return the element from.index
TSource
ArgumentNullException
source
is null.ArgumentOutOfRangeException
index
is less than 0 or greater than or equal to the number of elements in source
.This method enforces the use of zero-based indexing.
If the type of source
implements the IList<TSource>
interface, that implementation's get_Item(int)
method is used. Otherwise, this method iterates through the sequence and returns the element at the specified index.
using System;
using System.Collections.Generic;
using System.Linq;
public class Example
{
public static void Main(string[] args)
{
List<int> numbers = new List<int> { 5, 4, 3, 2, 1 };
// Get the element at index 2 (which is 3)
int element = numbers.ElementAt(2);
Console.WriteLine(element); // Output: 3
}
}