Accepted answer

You can use List.Remove, List.RemoveRange or List.RemoveAt methods to remove elements from the list.

To remove the last element of the list after you obtained a reference to it using the list indexer you use:

var element = myList[myList.Count - 1];
myList.RemoveAt(myList.Count - 1);

Using RemoveAt instead of Remove is more efficient because there is no need to first find the index of the item to be removed and also to note that Remove just removes the first occurrence that matches the item to be removed, so in a list with duplicates it will not be the last element.

Finally, if you have an algorithm where will be removing the last item of a list several times, you might as well consider another data structure, like for example a Queue.


You can do as following if you want to take the last.

string lastItem = myList.Last();
//myList.Count() will now be 4.


You can create an extension method that takes any index to do the fetch/removal:

public static T Extract<T>(this List<T> list, int index)
    var item = list[index];
    return item;

The last item can always be found using Count - 1, so the call is simply:

var item = list.Extract(list.Count - 1);

It then means you can "extract" any item by index.


Create an extension method to do this

public static class Extension
  public static string RemoveLast(this IList<string> myList)
     int lastItemIndex = myList.Count - 1;
     string lastItem = myList.Last();
     return lastItem; 

call it as

string itemRemoved = lst.RemoveLast(); //this will delete the last element from list & return last element as well 


Why not use a Stack instead of a List it seems more appropriate

public void MyTest()
    Stack<int> stack = new Stack<int>(new[] { 1, 2, 3, 4, 5 });
    int pop = stack.Pop();
    Assert.AreEqual(stack.Count, 4);
    Assert.AreEqual(pop, 5);

Related Articles