C# Linq ElementAt Method

The ElementAt() method in C# to get elements at specified index position.

Firstly, set the string array.

string[] str = { "Jack", "Pat", "David"};

Now, to get the element at a particular index, use the ElementAt() method as shown in the following example −


 Live Demo

using System.IO;
using System;
using System.Linq;
class Program {
   static void Main() {
      string[] str = { "Jack", "Pat", "David"};
      Random r = new Random(DateTime.Now.Second);
      // to generate random string
      string res = str.AsQueryable().ElementAt(r.Next(0, str.Length));
      Console.WriteLine("Random Name = '{0}'", res);


Random Name = 'Jack'
karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know