A foreach
statement enumerates the elements of a collection, executing a statement for each element of the collection.
The example
using System; using System.Collections; class Test { static void WriteList(ArrayList list) { foreach (object o in list) Console.WriteLine(o); } static void Main() { ArrayList list = new ArrayList(); for (int i = 0; i < 10; i++) list.Add(i); WriteList(list); } }
uses a foreach
statement to iterate over the elements of a list.