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 namespace System;
int main()
{
// Creates and initializes a new Array.
array<String^>^myArr = gcnew array<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 != nullptr) )
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
*/
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
설명
합니다 foreach C# 언어의 (for each c + +에서는 For Each Visual Basic에서) 열거자의 복잡성을 숨깁니다. 그러므로 열거자를 직접 조작하는 대신 foreach를 사용하는 것이 좋습니다.
열거자를 사용하여 컬렉션의 데이터를 읽을 수는 있지만 내부 컬렉션을 수정할 수는 없습니다.
처음에 열거자는 컬렉션의 첫 번째 요소 앞에 배치됩니다. Reset을 실행하면 열거자가 이 위치로 복원됩니다. 이 위치에서 Current는 정의되지 않습니다. 따라서 MoveNext의 값을 읽기 전에 Current를 호출하여 열거자를 해당 컬렉션의 첫 번째 요소로 보내야 합니다.
Current에서는 MoveNext 또는 Reset이 호출될 때까지 동일한 개체를 반환합니다. MoveNext는 Current를 다음 요소로 설정합니다.
경우 MoveNext 열거자를 컬렉션의 끝 컬렉션의 마지막 요소 뒤에 배치 되는 전달 하 고 MoveNext 반환 false합니다. 열거자가 있는 경우이 위치에 대 한 후속 호출은 MoveNext 반환할 수도 false합니다. 반환 falseCurrent 된 마지막 호출 MoveNext 이 정의되지 않은 경우 Current를 컬렉션의 첫째 요소에 다시 설정하려면 Reset을 호출한 뒤 MoveNext를 호출해야 합니다.
컬렉션이 변경되지 않고 그대로 유지되는 한 열거자는 유효한 상태로 유지됩니다. 컬렉션에 대해 변경 내용을 수행하면(예: 요소 추가, 수정 또는 삭제) 열거자는 복구 불가능하게 무효화되고 해당 동작은 정의되지 않습니다.
열거자는 컬렉션에 독점적으로 액세스할 수 있는 권한이 없으므로 컬렉션을 열거하는 프로시저는 기본적으로 스레드로부터 안전하지 않습니다. 열거 동안 스레드 보안을 보장하려면 전체 열거 동안 컬렉션을 잠그면 됩니다. 여러 스레드에서 컬렉션에 액세스하여 읽고 쓸 수 있도록 허용하려면 사용자 지정 동기화를 구현해야 합니다.
이 방법은 O(1) 작업에 설명 합니다.