Computer >> Computer tutorials >  >> Programming >> C#

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

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