Using foreach Statement with Array in C#

Using the foreach Statement 

1. Use a foreach statement to cycle through the elements of an array.

2. Each element in the array, one by one, allowing you to read its value

 

For example, the following code declares an array with four elements and then uses a foreach loop to print out the values of the items:

 

int[] arr1 = { 10, 11, 12, 13 }; // Define the array.

foreach (int item in arr1) // Enumerate the elements.

Console.WriteLine("Item value: {0}", item);

 

This code produces the following output:

Item value: 10

Item value: 11

Item value: 12

Item value: 13