C# Program to skip elements of an array from the end


Declare an array and initialize elements.

int[] marks = { 45, 50, 60, 70, 85 };

Use the SkipLast() method to skip elements of an array from the end.

IEnumerable<int> selMarks = marks.AsQueryable().SkipLast(3);

The elements are skipped and rest of the elements is returned as shown below −

Example

 Live Demo

using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      int[] marks = { 45, 50, 60, 70, 85 };
      Console.WriteLine("Array...");

      foreach (int res in marks)
      Console.WriteLine(res);
      IEnumerable<int> selMarks = marks.AsQueryable().SkipLast(3);
      Console.WriteLine("Array after skipping last 3 elements...");

      foreach (int res in selMarks)
      Console.WriteLine(res);
   }
}

Output

Array...
45
50
60
70
85
Array after skipping last 3 elements...
45
50

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 23-Jun-2020

857 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements