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 −

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);
   }
}

Output

Random Name = 'Jack'

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 23-Jun-2020

197 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements