ArrayList myArrayList = new ArrayList(); myArrayList.Add("apple"); myArrayList.Add("banana"); myArrayList.Add("orange"); IEnumerator myEnumerator = myArrayList.GetEnumerator(); while (myEnumerator.MoveNext()) { Console.WriteLine("Next element: " + myEnumerator.Current); }
ArrayList myArrayList = new ArrayList(); myArrayList.Add(1); myArrayList.Add(2); myArrayList.Add(3); IEnumerator myEnumerator = myArrayList.GetEnumerator(); for (int i = 0; i < myArrayList.Count; i++) { myEnumerator.MoveNext(); Console.WriteLine("Element at index " + i + ": " + myEnumerator.Current); }In this example, an ArrayList is created and filled with three integer objects. An enumerator is obtained from the ArrayList and used within a for loop to loop through the elements using the MoveNext method. The current element is then printed to the console along with its index. The System.Collections.ArrayList class is part of the System.Collections namespace in the .NET Framework.