Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0083 foreach loop

foreach loop is for the enumerable object. An array is an enumerable object. The following code uses the foreach to print out the value for each element in an array. using System; class Program { static void Main(string[] args) { int[] intArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }; foreach (int i in intArray) { Console.WriteLine(i); } } } The output: 1 2 3 4 5 6 7 8