This post will discuss how to remove the first item from a list in C#.

1. Using List<T>.RemoveAt() Method

The RemoveAt() method remove element present at the specified position in the list. The idea is to pass the zero-index to the RemoveAt() method to remove the first element from it.

Download  Run Code

 
The RemoveAt() method throws an ArgumentOutOfRangeException if the specified index is out of range of valid indices. The following code example shows how to handle this:

Download  Run Code

2. Using List<T>.RemoveRange() Method

The List<T>.RemoveRange() method is used to remove a range of elements from a List<T>. It can be used as follows to remove only the first element from the list. The solution can be easily extended to remove a range of elements from the list.

Download  Run Code

 
Note that the RemoveRange() method throws an exception if the index and count do not denote a valid range of elements in the list. This can be handled by doing a range check before invoking the RemoveRange() method.

3. Using LinkedList<T>.RemoveFirst() Method

If you’re using a LinkedList<T>, you can use the RemoveFirst() method to remove the node at the start of the linked list in O(1) time. For example,

Download  Run Code

That’s all about removing the first item from a list in C#.