다음을 통해 공유


Array.GetEnumerator 메서드

Array에 대한 IEnumerator를 반환합니다.

네임스페이스: System
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Public Function GetEnumerator As IEnumerator
‘사용 방법
Dim instance As Array
Dim returnValue As IEnumerator

returnValue = instance.GetEnumerator
public IEnumerator GetEnumerator ()
public:
virtual IEnumerator^ GetEnumerator () sealed
public final IEnumerator GetEnumerator ()
public final function GetEnumerator () : IEnumerator

반환 값

Array에 대한 IEnumerator입니다.

설명

C# 언어의 foreach 문(C++의 경우 for each, Visual Basic의 경우 For Each)은 열거자를 덜 복잡하게 표시합니다. 따라서 열거자를 직접 조작하는 것보다는 foreach를 사용하는 것이 좋습니다.

열거자는 컬렉션에 있는 데이터를 읽는 데 사용할 수 있지만 내부 컬렉션을 수정하는 데에는 사용할 수 없습니다.

처음에는 열거자가 컬렉션의 첫 번째 요소 앞에 배치됩니다. 그러면 Reset은 열거자를 이 위치로 다시 가져옵니다. 이 위치에서 Current는 정의되지 않습니다. 따라서 Current의 값을 읽기 전에 MoveNext를 호출하여 열거자를 해당 컬렉션의 첫 번째 요소로 보내야 합니다.

Current에서는 MoveNext 또는 Reset이 호출될 때까지 동일한 개체를 반환합니다. MoveNextCurrent를 다음 요소로 설정합니다.

MoveNext가 컬렉션의 끝을 지나게 되면 열거자는 컬렉션의 마지막 요소 뒤에 배치되고, MoveNextfalse를 반환합니다. 열거자가 이 위치에 있는 경우 MoveNext에 대한 후속 호출 또한 false를 반환합니다. MoveNext에 대한 마지막 호출에서 false가 반환된 경우 Current는 정의되지 않습니다. Current를 해당 컬렉션의 첫 번째 요소로 다시 설정하려면 Reset을 호출한 다음 MoveNext를 호출하면 됩니다.

열거자는 컬렉션이 변경되지 않은 상태로 유지되는 한 유효합니다. 요소를 추가, 수정 또는 삭제하는 등 컬렉션을 변경하면 열거자가 더 이상 유효하지 않게 되며(복구할 수 없음) 해당 동작은 정의되지 않은 상태가 됩니다.

열거자는 컬렉션에 독점적으로 액세스할 수 있는 권한이 없으므로 컬렉션을 열거하는 프로시저는 기본적으로 스레드로부터 안전하지 않습니다. 열거하는 동안 스레드로부터 안전하게 보호하려면 열거하는 동안 컬렉션을 잠글 수 있습니다. 여러 스레드에서 컬렉션에 액세스하여 읽고 쓸 수 있도록 허용하려면 사용자 지정 동기화를 구현해야 합니다.

이 메서드는 O(1) 연산입니다.

예제

다음 코드 예제에서는 GetEnumerator를 사용하여 배열의 요소를 나열하는 방법을 보여 줍니다.

Imports System

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) = "jumped"
      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 'Main

End Class 'SamplesArray 


'This code produces the following output.
'
'The Array contains the following values:
'[0] The
'[1] quick
'[2] brown
'[3] fox
'[4] jumped
'[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] = "jumped";
      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] jumped
[5] over
[6] the
[7] lazy
[8] dog

*/
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 ] = "jumped";
   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] jumped
[5] over
[6] the
[7] lazy
[8] dog

*/
import System.*;

public class SamplesArray
{
    public static void main(String[] args)
    {
        // Creates and initializes a new Array.
        String myArr[] = new String[10];

        myArr.set_Item(0, "The");
        myArr.set_Item(1, "quick");
        myArr.set_Item(2, "brown");
        myArr.set_Item(3, "fox");
        myArr.set_Item(4, "jumped");
        myArr.set_Item(5, "over");
        myArr.set_Item(6, "the");
        myArr.set_Item(7, "lazy");
        myArr.set_Item(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.get_Current() != null)) {
            Console.WriteLine("[{0}] {1}", System.Convert.ToString(i++), 
                myEnumerator.get_Current());
        }
    } //main
} //SamplesArray
 /* 
This code produces the following output.

The Array contains the following values:
[0] The
[1] quick
[2] brown
[3] fox
[4] jumped
[5] over
[6] the
[7] lazy
[8] dog

*/

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

Array 클래스
Array 멤버
System 네임스페이스