Remove last element from a List in C#
This post will discuss how to remove the last element from a List in C#.
1. Using List<T>.RemoveAt()
Method
The RemoveAt() method removes the element present at the specified position in a list. To remove the last element, you can pass the index of it to the RemoveAt()
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; using System.Collections.Generic; public class Example { public static void Main() { List<string> strings = new List<string>() { "A", "B", "C", "D" }; strings.RemoveAt(strings.Count - 1); Console.WriteLine(String.Join(", ", strings)); // A, B, C } } |
The above solution throws System.IndexOutOfRangeException
for an empty list. You can easily handle this by checking the number of elements in the list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; using System.Collections.Generic; public class Example { public static void Main() { List<string> strings = new List<string>() { "A", "B", "C", "D" }; if (strings.Count > 0) { strings.RemoveAt(strings.Count - 1); } Console.WriteLine(String.Join(", ", strings)); } } |
Starting with C# 8.0, you can use the RemoveAt(^1)
method instead of the RemoveAt(Count - 1)
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; using System.Collections.Generic; public class Example { public static void Main() { List<string> strings = new List<string>() { "A", "B", "C", "D" }; strings.RemoveAt(^1); Console.WriteLine(String.Join(", ", strings)); // A, B, C } } |
2. Using LinkedList<T>.RemoveLast()
Method
To remove an element from the end of the LinkedList<T>
, consider using the RemoveLast()
method. A linked list provides a more complete and consistent set of LIFO and FIFO operations. Here’s what the code would look like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.Collections.Generic; public class Example { public static void Main() { LinkedList<string> strings = new LinkedList<string>(); strings.AddLast("A"); strings.AddLast("B"); strings.AddLast("C"); strings.AddLast("D"); strings.RemoveLast(); Console.WriteLine(String.Join(", ", strings)); // A, B, C } } |
3. Using List<T>.RemoveRange()
Method
The List<T>.RemoveRange() method is used to remove a range of elements from a List<T>
. It takes the starting index of the range of elements to remove and the number of elements to remove. It can be used as follows to remove the last index. The solution can be easily extended to remove the last n
elements from a list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; using System.Collections.Generic; public class Example { public static void Main() { List<string> strings = new List<string>() { "A", "B", "C", "D" }; if (strings.Count > 0) { strings.RemoveRange(strings.Count - 1, 1); } Console.WriteLine(String.Join(", ", strings)); // A, B, C } } |
4. Using Enumerable.Take()
Method
The following solution uses the Take()
method to create a new list having all the elements of the original list except its last, and does not modify the original list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<string> strings = new List<string>() { "A", "B", "C", "D" }; List<string> filtered = strings.Take(strings.Count() - 1).ToList(); Console.WriteLine(String.Join(", ", filtered)); // A, B, C } } |
That’s all about removing the last element from a List in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)