This post will discuss how to loop through a List in C#.

1. Using foreach Statement

The standard option to iterate over the List in C# is using a foreach loop. Then, we can perform any action on each element of the List. The following code example demonstrates its usage by displaying the contents of the list to the console.

Download  Run Code

Output:

1
2
3
4
5

2. Using List<T>.ForEach

Another good alternative to iterate through a list is using List<T>.ForEach(Action<T>) method. It performs the specified action on each element of the list. It accepts a delegate to perform on each element of the List. The following example shows how to print the contents of a list using the Action<T> delegate.

Download  Run Code

Output:

1
2
3
4
5

3. Using For Loop

Finally, we can replace a foreach loop with an index-based for-loop, as the following example illustrates.

Download  Run Code

Output:

1
2
3
4
5

That’s all about looping through a List in C#.