Get Last Element from an Array Using C#



Declare an array and add elements.

int[] val = { 5, 8, 15, 25, 40, 55, 80, 100 };

Now, use the Queryable Last() method to get the last element.

val.AsQueryable().Last();

Let us see the complete code.

Example

 Live Demo

using System;
using System.Collections.Generic;
using System.Linq;
class Demo {
   static void Main() {
      int[] val = { 5, 8, 15, 25, 40, 55, 80, 100 };
      int last_num = val.AsQueryable().Last();
      Console.WriteLine("Last element: "+last_num);
   }
}

Output

Last element: 100
Updated on: 2020-06-22T15:14:13+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements