ZetCode

C# foreach

last modified July 5, 2020

C# foreach tutorial shows how to loop over data in C# with foreach statement and forEach method. C# tutorial is a comprehensive tutorial on C# language.

The foreach statement executes a statement or a block of statements for each element in a collection which implements IEnumerable.

public void ForEach (Action<T> action);

The ForEach method performs the specified action on each element of the List<T>.

C# foreach array

In the following example, we go over elements of an array using the foreach statement.

Program.cs
using System;

namespace ForeachStatementEx
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] vals = {1, 2, 3, 4, 5};

            foreach (var val in vals)
            {
                Console.WriteLine(val);
            }
        }
    }
}

The example prints all elements of the array of integers.

$ dotnet run
1
2
3
4
5

This is the output.

C# foreach List

In the following example, we loop over a list with the foreach statement.

Program.cs
using System;
using System.Collections.Generic;

namespace ForeachStatementEx2
{
    class Program
    {
        static void Main(string[] args)
        {
            var words = new List<string> {"tea", "falcon", "book", "sky"};

            foreach (var word in words)
            {
                Console.WriteLine(word);
            }
        }
    }
}

We print all elements of the list of strings.

$ dotnet run
tea
falcon
book
sky

This is the output.

C# foreach Dictionary

The following example loops over elements of a dictionary with foreach statement.

Program.cs
using System;
using System.Collections.Generic;

namespace ForeachStatementEx3
{
    class Program
    {
        static void Main(string[] args)
        {
            var domains = new Dictionary<string, string>
            { 
                {"sk", "Slovakia"}, 
                {"ru", "Russia"},
                {"de", "Germany"},
                {"no", "Norway"}
            };

            foreach (var pair in domains)
            {
                Console.WriteLine($"{pair.Key} - {pair.Value}");
            }
            
            Console.WriteLine("-----------------------");

            foreach ((var Key, var Value) in domains) 
            {
                Console.WriteLine($"{Key} - {Value}");
            }
        }
    }
}

We have a dictionary of domains. Using the foreach statement, we loop over the key/value pairs of the dictionary.

$ dotnet run
sk - Slovakia
ru - Russia
de - Germany
no - Norway
-----------------------
sk - Slovakia
ru - Russia
de - Germany
no - Norway

This is the output.

C# forEach array

In the following example, we go over elements of an array using the forEach method.

Program.cs
using System;

namespace ForeachMethodEx
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] vals = {1, 2, 3, 4, 5};

            Array.ForEach(vals, e => Console.WriteLine(e));
        }
    }
}

The example prints all elements of the array of integers. We use the Array.ForEach method.

C# forEach List

In the following example, we loop over a list with the forEach method.

Program.cs
using System;
using System.Collections.Generic;

namespace ForeachMethodEx2
{
    class Program
    {
        static void Main(string[] args)
        {
            var words = new List<string> {"tea", "falcon", "book", "sky"};

            words.ForEach(e => Console.WriteLine(e));
        }
    }
}

We print all elements of the list of strings.

C# foreach Dictionary

The following example loops over elements of a dictionary with ForEach. In this case we also use Linq.

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;

namespace ForeachMethodEx3
{
    class Program
    {
        static void Main(string[] args)
        {
            var domains = new Dictionary<string, string>
            { 
                {"sk", "Slovakia"}, 
                {"ru", "Russia"},
                {"de", "Germany"},
                {"no", "Norway"}
            };

            domains.ToList().ForEach(pair => Console.WriteLine($"{pair.Key} - {pair.Value}"));
        }
    }
}

We call Linq's ToList method to transform the dictionary to a list and then call the ForEach method.

In this tutorial we have worked covered the foreach statement and the forEach method in C#.

Read C# tutorial or list all C# tutorials.