C#中的Array.GetEnumerator方法
C#中的Array.GetEnumerator()方法用于返回Array的IEnumerator。
语法
以下是语法-
public virtual System.Collections.IEnumerator GetEnumerator ();
示例
现在让我们看一个实现Array.GetEnumerator()方法的示例-
using System; using System.Collections; public class Demo{ public static void Main(){ int j = 0; Console.WriteLine("Array elements..."); string[] arr = { "car", "bike", "truck", "bus"}; for (int i = 0; i < arr.Length; i++){ Console.Write("{0} ", arr[i]); } Console.WriteLine(); IEnumerator newEnum = arr.GetEnumerator(); Console.WriteLine("IEnumerator for the Array..."); while ((newEnum.MoveNext()) && (newEnum.Current != null)) { Console.WriteLine("[{0}] {1}", j++, newEnum.Current); } } }
输出结果
这将产生以下输出-
Array elements... car bike truck bus IEnumerator for the Array... [0] car [1] bike [2] truck [3] bus