This post will discuss how to iterate backwards in a List in C#.

1. Using for loop

A simple solution to iterate backwards in a list in C# is using a regular for-loop. The idea is to start from the last index to the first index and process each item. This would translate to the following code:

Download  Run Code

Output:

5
4
3
2
1

 
Alternatively, you can create an extension method for getting a sequence of elements in the list in reverse order. Here’s what the code would look like:

Download  Run Code

Output:

5
4
3
2
1

2. Using foreach loop

An alternative way to iterate backwards in a list is to reverse the list with the List.Reverse() method and iterate over the reversed list using the foreach statement. Note that this approach is not recommended as it changes the original order of the list.

Download  Run Code

Output:

5
4
3
2
1

That’s all about iterating backwards in a List in C#.