C# Program to display the last three elements from a list in reverse order


To display the last three elements from a list, use the Take() method. For reversing it, use the Reverse() method.

Firstly, declare a list and add elements to it −

List<string> myList = new List<string>();
myList.Add("One");
myList.Add("Two");
myList.Add("Three");
myList.Add("Four");

Now, use the Take() method with Reverse() to display the last three elements from the list in reverse order −

myList.Reverse<string>().Take(3);

The following is the code −

Example

 Live Demo

using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      List<string> myList = new List<string>();
      myList.Add("One");
      myList.Add("Two");
      myList.Add("Three");
      myList.Add("Four");
      // first three elements
      var res = myList.Reverse<string>().Take(3);
      // displaying last three elements
      foreach (string str in res) {
         Console.WriteLine(str);
      }
   }
}

Output

Four
Three
Two

Updated on: 22-Jun-2020

969 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements