C# Program to return specified number of elements from the end of an array

CsharpProgrammingServer Side Programming

Use the TakeLast() method to return elements from the end of an array.

Let us first declare and initialize an array.

int[] prod = { 110, 290, 340, 540, 456, 698, 765, 789};

Now, let’s get the last three elements.

IEnumerable<int> units = prod.AsQueryable().TakeLast(3);

Let us see the complete code.

Example

 Live Demo

using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      int[] prod = { 110, 290, 340, 540, 456, 698, 765, 789};
      // value of last three products
      IEnumerable<int> units = prod.AsQueryable().TakeLast(3);
      foreach (int res in units) {
         Console.WriteLine(res);
      }
   }
}

Output

698
765
789
raja
Published on 12-Sep-2018 12:15:54
Advertisements