C# Linq Last() Method


Get the last element from a sequence using the Linq Last() method.

The following is our array.

int[] val = { 10, 20, 30, 40 };

Now, get the last element.

val.AsQueryable().Last();

Example

 Live Demo

using System;
using System.Collections.Generic;
using System.Linq;
class Demo {
   static void Main() {
      int[] val = { 10, 20, 30, 40 };
      // getting last element
      int last_num = val.AsQueryable().Last();
      Console.WriteLine("Last element: "+last_num);
   }
}

Output

Last element: 40

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 23-Jun-2020

926 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements