C# Enumerable ElementAt
Description
Returns the element at a specified index in a sequence.
Syntax
public static TSource ElementAt<TSource>(
this IEnumerable<TSource> source,
int index
)
Parameters
TSource
- The type of the elements of source.source
- An IEnumerableto return an element from. index
- The zero-based index of the element to retrieve.
Returns
Returns the element at a specified index in a sequence.
Example
The following code example demonstrates how to use ElementAt to return an element at a specific position.
//from w w w .j ava 2 s. c o m
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
public static void Main(String[] argv){
string[] names ={ "A", "B", "C", };
Random random = new Random(DateTime.Now.Millisecond);
string name = names.ElementAt(random.Next(0, names.Length));
Console.WriteLine(name);
}
}
The code above generates the following result.