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