Iterate Through a List in C#

  1. Iterate Through a List With the for Loop in C
  2. Iterate Through a List With the foreach Loop in C
  3. Iterate Through a List With the Lambda Expressions in C

This tutorial will discuss the methods to iterate through a list in C#.

Iterate Through a List With the for Loop in C

The for loop repeats a block of code for a specified amount of time in C#. We can use the for loop to iterate through a list in C#. See the following example.

using System;
using System.Collections.Generic;

namespace iterate_through_a_list
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> values = new List<string> { "value1", "value2", "value3" };
            for(int i = 0; i < values.Count; i++)
            {
                Console.WriteLine("Element#{0} = {1}", i, values[i]);
            }
        }
    }
}

Output:

Element#0 = value1
Element#1 = value2
Element#2 = value3

We initialized the list values containing string values and iterated through values with the for loop in C#. We used the values.Count property as the upper limit of the loop and printed all the values list elements.

Iterate Through a List With the foreach Loop in C

The foreach loop iterates through a data structure in C#. The foreach loop is used as an iterator because it repeats a code block for each element inside the data structure. We can also use the foreach loop to iterate through a list. The following code example shows us how we can iterate through a list with the foreach loop in C#.

using System;
using System.Collections.Generic;

namespace iterate_through_a_list
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> values = new List<string> { "value1", "value2", "value3" };
            foreach(var v in values)
            {
                Console.WriteLine("Element = {0}", v);
            }
        }
    }
}

Output:

Element = value1
Element = value2
Element = value3

We initialized the list values containing string values and iterated through values with the foreach loop in C#. We do not have to specify any upper limit in foreach loop. It automatically loops through each element of a data structure in C#.

Iterate Through a List With the Lambda Expressions in C

To make our code more concise, we can also use the lambda expressions to iterate through a list in C#. The lambda expressions create anonymous functions in C#. We can create an anonymous function that iterates through our list with the lambda expressions in C#. The following code example shows us how we can iterate through a list with the lambda expressions in C#

using System;
using System.Collections.Generic;

namespace iterate_through_a_list
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> values = new List<string> { "value1", "value2", "value3" };
            values.ForEach((v) => Console.WriteLine("Element = {0}", v));
        }
    }
}

Output:

Element = value1
Element = value2
Element = value3

In the above code, we initialized the list values containing string values and iterated through values with lambda expressions in C#. The lambda expressions reduced our iterative code to just one line.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Csharp List

  • Get the Last Element of a List in C#
  • Declare a Multidimensional List in C#