Hit enter after type your search item

What is foreach loop in C#?

The C# foreach statement is used to iterate through the elements of collections, arrays etc. You may execute a single statement or a block of statements for each element as using the foreach statement.

The general structure for using the foreach:

The example of foreach with an array

For explaining how foreach works, we have an array of four elements. This is a numeric array and value is assigned to each element.

A foreach loop is used and that array is specified. A simple statement is used to display the current element’s value in each iteration:


The output of the above code:

c# foreach

Info: Where you may use foreach statement? An instance of the type that implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable<T> interface e.g. List. You may also use foreach with the instance of any type that has the public parameterless GetEnumerator method.

The example of using foreach with a list

Similarly, you may use the foreach loop to iterate through the list collection in C#. In the example below, we have created a list of numeric items. Then, a foreach loop is used to iterate through the listnums and in each loop, we will display the current list item by Console.WriteLine statement. Have a look:


The output:

c# foreach-list

An example of leap year using foreach

For this example, a list of years is created. The list is assigned nine years (from 1992 to 2108). This is followed by using the foreach statement where if statement is used and each year is checked whether it is a leap year or not.

The code:


The output:

c# foreach-leap

You can see, the list contains following years:

1992, 1994, 1998, 2001, 2004, 2008, 2014, 2016, 2018

The program returned the leap years only.

Exit foreach with a break statement

At any point, you may exit the foreach loop by using the break statement. The current iteration can also be omitted by the continue statement (see next example).

For showing how break statement in foreach works, we have a list of five elements. As the current element value is equal to 15, the break statement executes inside the foreach that exits the foreach loop:


c# foreach-break

You can see, as the value of list item is 15 the if statement evaluated as true and it executed the break statement that terminated the foreach.

The example of using continue statement

The following example uses continue statement for omitting the current iteration in foreach loop. Again, we have a numeric array of six elements and execute the foreach loop.

The array contains following elements:

5, 7, 9, 11, 13, 15

Inside the foreach, the current element is displayed by Console.WriteLine method. However, as it reaches the number 13, the continue statement executes and we will not display that value. Have a look:


c# foreach-continue

You can see, all elements are displayed except the number 13.

This div height required for enabling the sticky sidebar