C# foreach array with Code Examples

Last Updated On Saturday 13th Nov 2021
  • An array is also a collection, which means that you can iterate over all its values using a for each loop.
  • A for each loop is guaranteed to iterate over all values.

c# loop through the array

Example 1

	using System;  
public class ArrayExample  
{  
    public static void Main(string[] args)  
    {  
        int[] arr = { 10, 20, 30, 40, 50, 60, 70};

        foreach (int i in arr)  
        {  
            Console.WriteLine(i);  
        }  
    }  
}  
	

The Above Outputs

	// 10
// 20
// 30
// 40
// 50
// 60
// 70
	

Example 2

	using System;
public class ArrayExample {
  public static void Main(string[] args) {
    char[] vowels = new [] {
      'a',
      'e',
      'i',
      'o',
      'u'
    };

    foreach(char vowel in vowels) {
      // Output the vowel
      System.Console.Write(vowel);
    }

  }
}
	

The Above Outputs

	// aeiou