C# foreach
last modified January 17, 2024
In this article we show how to loop over data in C# with foreach
statement and forEach method.
foreach (var val in vals)
{
...
}
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.
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
C# foreach two-dimensional array
The next example loops over a two-dimensional array.
int[,] vals =
{
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15}
};
foreach (var e in vals)
{
Console.Write($"{e} ");
}
Console.WriteLine();
Console.WriteLine("------------------------------------");
foreach (int i in Enumerable.Range(0, vals.GetLength(0)))
{
foreach (int j in Enumerable.Range(0, vals.GetLength(1)))
{
Console.Write($"{vals[i, j]}");
}
Console.WriteLine();
}
We have a two-dimensional array of integers. We loop over the values twice.
foreach (var e in vals)
{
Console.Write($"{e} ");
}
Here, we loop over the elements one by one.
foreach (int i in Enumerable.Range(0, vals.GetLength(0)))
{
foreach (int j in Enumerable.Range(0, vals.GetLength(1)))
{
Console.Write($"{vals[i, j]}");
}
Console.WriteLine();
}
Using two foreach loops, we loop over the nested arrays.
$ dotnet run 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ------------------------------------ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
C# foreach List
In the following example, we loop over a list with the foreach
statement.
List<string> words = ["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
C# foreach list of lists
In the next example, we loop over a list of lists.
List<List<string>> words =
[
new() {"tea", "falcon", "book", "sky"},
new() {"cup", "crown", "borrow", "moore"},
new() {"arm", "nice", "frost", "sea"}
];
foreach (var nested in words)
{
foreach (var word in nested)
{
Console.WriteLine(word);
}
}
To iterate over a list of lists, we use two foreach loops.
C# foreach Dictionary
The following example loops over elements of a dictionary with
foreach statement.
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
C# forEach array
In the following example, we go over elements of an array using the
forEach method.
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.
List<string> words = ["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.
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.
Source
In this article we have covered the foreach statement and the
forEach method in C#.
Author
List all C# tutorials.