A foreach loop can be used to iterate over each of the elements in a collection. The collection can be held in an array or in a parameterized container, like a List, as shown in the program. Due to the nature of foreach loops, elements may not be added or removed during its execution in order to prevent the erractic behavior that would result.
using System; using System.Collections.Generic; namespace XoaX { class Program { static void Main(string[] args) { // Array int[] iaSacredNumbers = { 40, 7, 12, 3 }; foreach (int i in iaSacredNumbers) { Console.Write(i + " "); } Console.WriteLine(); // List List<string> qProphets = new List<string>(); qProphets.Add("Isaiah"); qProphets.Add("Jeremiah"); qProphets.Add("Daniel"); qProphets.Add("Ezekiel"); qProphets.Add("Jonah"); qProphets.Add("Elijah"); foreach (string sProphet in qProphets) { Console.Write(sProphet + " "); } Console.WriteLine(); } } }
40 7 12 3 Isaiah Jeremiah Daniel Ezekiel Jonah Elijah Press any key to continue . . .
© 20072024 XoaX.net LLC. All rights reserved.