C# Linq First() Method


Use the First() method to get the first element from an array.

Firstly, set an array.

int[] arr = {20, 40, 60, 80 , 100};

Now, use the Queryable First() method to return the first element.

arr.AsQueryable().First();

The following is the entire example.

Example

 Live Demo

using System;
using System.Linq;
using System.Collections.Generic;
class Program {
   static void Main() {
      int[] arr = {20, 40, 60, 80 , 100};
      // getting the first element
      int res = arr.AsQueryable().First();
      Console.WriteLine(res);
   }
}

Output

20

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 23-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements