Array.GetEnumerator 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
에 대한 값을 IEnumerator 반환합니다 Array.
public:
virtual System::Collections::IEnumerator ^ GetEnumerator();
public System.Collections.IEnumerator GetEnumerator();
public virtual System.Collections.IEnumerator GetEnumerator();
abstract member GetEnumerator : unit -> System.Collections.IEnumerator
override this.GetEnumerator : unit -> System.Collections.IEnumerator
Public Function GetEnumerator () As IEnumerator
Public Overridable Function GetEnumerator () As IEnumerator
반품
에 대한 항목 IEnumerator 입니다 Array.
구현
예제
다음 코드 예제에서는 배열의 요소를 나열하는 데 사용하는 GetEnumerator 방법을 보여줍니다.
using System;
public class SamplesArray {
public static void Main() {
// Creates and initializes a new Array.
String[] myArr = new String[10];
myArr[0] = "The";
myArr[1] = "quick";
myArr[2] = "brown";
myArr[3] = "fox";
myArr[4] = "jumps";
myArr[5] = "over";
myArr[6] = "the";
myArr[7] = "lazy";
myArr[8] = "dog";
// Displays the values of the Array.
int i = 0;
System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
Console.WriteLine( "The Array contains the following values:" );
while (( myEnumerator.MoveNext() ) && ( myEnumerator.Current != null ))
Console.WriteLine( "[{0}] {1}", i++, myEnumerator.Current );
}
}
/*
This code produces the following output.
The Array contains the following values:
[0] The
[1] quick
[2] brown
[3] fox
[4] jumps
[5] over
[6] the
[7] lazy
[8] dog
*/
// Creates and initializes a new Array.
let myArr = Array.zeroCreate 10
myArr[0..8] <-
[| "The"
"quick"
"brown"
"fox"
"jumps"
"over"
"the"
"lazy"
"dog" |]
// Displays the values of the Array.
let mutable i = 0
let myEnumerator = myArr.GetEnumerator()
printfn "The Array contains the following values:"
while myEnumerator.MoveNext() && myEnumerator.Current <> null do
printfn $"[{i}] {myEnumerator.Current}"
i <- i + 1
// This code produces the following output.
// The Array contains the following values:
// [0] The
// [1] quick
// [2] brown
// [3] fox
// [4] jumps
// [5] over
// [6] the
// [7] lazy
// [8] dog
Public Class SamplesArray
Public Shared Sub Main()
' Creates and initializes a new Array.
Dim myArr(10) As [String]
myArr(0) = "The"
myArr(1) = "quick"
myArr(2) = "brown"
myArr(3) = "fox"
myArr(4) = "jumps"
myArr(5) = "over"
myArr(6) = "the"
myArr(7) = "lazy"
myArr(8) = "dog"
' Displays the values of the Array.
Dim i As Integer = 0
Dim myEnumerator As System.Collections.IEnumerator = myArr.GetEnumerator()
Console.WriteLine("The Array contains the following values:")
While myEnumerator.MoveNext() And Not (myEnumerator.Current Is Nothing)
Console.WriteLine("[{0}] {1}", i, myEnumerator.Current)
i += 1
End While
End Sub
End Class
'This code produces the following output.
'
'The Array contains the following values:
'[0] The
'[1] quick
'[2] brown
'[3] fox
'[4] jumps
'[5] over
'[6] the
'[7] lazy
'[8] dog
설명
C# 언어의 foreach 문(Visual Basic For Each)은 열거자의 복잡성을 숨깁니다. 따라서 열거자를 직접 조작하는 대신 사용하는 foreach 것이 좋습니다.
열거자는 컬렉션의 데이터를 읽는 데 사용할 수 있지만 기본 컬렉션을 수정하는 데 사용할 수는 없습니다.
처음에는 열거자가 컬렉션의 첫 번째 요소 앞에 배치됩니다. Reset 또한 열거자를 이 위치로 다시 가져옵니다. 이 위치에서 Current 정의되지 않습니다. 따라서 값을 MoveNext읽기 전에 열거자를 컬렉션의 첫 번째 요소로 이동하도록 호출 Current 해야 합니다.
Current 는 호출될 때까지 동일한 개체를 MoveNextReset 반환합니다. MoveNext 는 다음 요소로 설정 Current 됩니다.
컬렉션의 끝을 전달하면 MoveNext 열거자가 컬렉션의 마지막 요소 뒤 위치에 배치되고 MoveNext 반환됩니다 false. 열거자가 이 위치에 있으면 후속 호출도 반환MoveNext합니다false. 반환 MoveNextfalse 된 Current 마지막 호출이 정의되지 않은 경우 컬렉션의 첫 번째 요소로 다시 설정 Current 하려면 다음Reset을 호출 MoveNext 할 수 있습니다.
컬렉션이 변경되지 않은 상태로 유지되는 한 열거자는 유효한 상태로 유지됩니다. 요소 추가, 수정 또는 삭제와 같이 컬렉션이 변경되면 열거자가 복구할 수 없게 무효화되고 해당 동작이 정의되지 않습니다.
열거자는 컬렉션에 대한 단독 액세스 권한이 없습니다. 따라서 컬렉션을 열거하는 것은 본질적으로 스레드로부터 안전한 프로시저가 아닙니다. 열거 중 스레드 안전을 보장하기 위해 전체 열거형 중에 컬렉션을 잠글 수 있습니다. 읽기 및 쓰기를 위해 여러 스레드에서 컬렉션에 액세스할 수 있도록 하려면 고유한 동기화를 구현해야 합니다.
이 메서드는 O(1) 작업입니다.