StringDictionary.GetEnumerator 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
문자열 사전을 반복하는 열거자를 반환합니다.
public:
virtual 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 Overridable Function GetEnumerator () As IEnumerator
반품
IEnumerator 문자열 사전을 반복하는 값입니다.
구현
예제
다음 코드 예제에서는 .의 요소를 열거합니다 StringDictionary.
using System;
using System.Collections;
using System.Collections.Specialized;
public class SamplesStringDictionary {
public static void Main() {
// Creates and initializes a new StringDictionary.
StringDictionary myCol = new StringDictionary();
myCol.Add( "red", "rojo" );
myCol.Add( "green", "verde" );
myCol.Add( "blue", "azul" );
// Display the contents of the collection using foreach. This is the preferred method.
Console.WriteLine( "Displays the elements using foreach:" );
PrintKeysAndValues1( myCol );
// Display the contents of the collection using the enumerator.
Console.WriteLine( "Displays the elements using the IEnumerator:" );
PrintKeysAndValues2( myCol );
// Display the contents of the collection using the Keys, Values, Count, and Item properties.
Console.WriteLine( "Displays the elements using the Keys, Values, Count, and Item properties:" );
PrintKeysAndValues3( myCol );
}
// Uses the foreach statement which hides the complexity of the enumerator.
// NOTE: The foreach statement is the preferred way of enumerating the contents of a collection.
public static void PrintKeysAndValues1( StringDictionary myCol ) {
Console.WriteLine( " KEY VALUE" );
foreach ( DictionaryEntry de in myCol )
Console.WriteLine( " {0,-25} {1}", de.Key, de.Value );
Console.WriteLine();
}
// Uses the enumerator.
// NOTE: The foreach statement is the preferred way of enumerating the contents of a collection.
public static void PrintKeysAndValues2( StringDictionary myCol ) {
IEnumerator myEnumerator = myCol.GetEnumerator();
DictionaryEntry de;
Console.WriteLine( " KEY VALUE" );
while ( myEnumerator.MoveNext() ) {
de = (DictionaryEntry) myEnumerator.Current;
Console.WriteLine( " {0,-25} {1}", de.Key, de.Value );
}
Console.WriteLine();
}
// Uses the Keys, Values, Count, and Item properties.
public static void PrintKeysAndValues3( StringDictionary myCol ) {
String[] myKeys = new String[myCol.Count];
myCol.Keys.CopyTo( myKeys, 0 );
Console.WriteLine( " INDEX KEY VALUE" );
for ( int i = 0; i < myCol.Count; i++ )
Console.WriteLine( " {0,-5} {1,-25} {2}", i, myKeys[i], myCol[myKeys[i]] );
Console.WriteLine();
}
}
/*
This code produces the following output.
Displays the elements using foreach:
KEY VALUE
red rojo
blue azul
green verde
Displays the elements using the IEnumerator:
KEY VALUE
red rojo
blue azul
green verde
Displays the elements using the Keys, Values, Count, and Item properties:
INDEX KEY VALUE
0 red rojo
1 blue azul
2 green verde
*/
Imports System.Collections
Imports System.Collections.Specialized
Public Class SamplesStringDictionary
Public Shared Sub Main()
' Creates and initializes a new StringDictionary.
Dim myCol As New StringDictionary()
myCol.Add("red", "rojo")
myCol.Add("green", "verde")
myCol.Add("blue", "azul")
' Display the contents of the collection using For Each. This is the preferred method.
Console.WriteLine("Displays the elements using For Each:")
PrintKeysAndValues1(myCol)
' Display the contents of the collection using the enumerator.
Console.WriteLine("Displays the elements using the IEnumerator:")
PrintKeysAndValues2(myCol)
' Display the contents of the collection using the Keys, Values, Count, and Item properties.
Console.WriteLine("Displays the elements using the Keys, Values, Count, and Item properties:")
PrintKeysAndValues3(myCol)
End Sub
' Uses the For Each statement which hides the complexity of the enumerator.
' NOTE: The For Each statement is the preferred way of enumerating the contents of a collection.
Public Shared Sub PrintKeysAndValues1(myCol As StringDictionary)
Console.WriteLine(" KEY VALUE")
Dim de As DictionaryEntry
For Each de In myCol
Console.WriteLine(" {0,-25} {1}", de.Key, de.Value)
Next de
Console.WriteLine()
End Sub
' Uses the enumerator.
' NOTE: The For Each statement is the preferred way of enumerating the contents of a collection.
Public Shared Sub PrintKeysAndValues2(myCol As StringDictionary)
Dim myEnumerator As IEnumerator = myCol.GetEnumerator()
Dim de As DictionaryEntry
Console.WriteLine(" KEY VALUE")
While myEnumerator.MoveNext()
de = CType(myEnumerator.Current, DictionaryEntry)
Console.WriteLine(" {0,-25} {1}", de.Key, de.Value)
End While
Console.WriteLine()
End Sub
' Uses the Keys, Values, Count, and Item properties.
Public Shared Sub PrintKeysAndValues3(myCol As StringDictionary)
Dim myKeys(myCol.Count) As String
myCol.Keys.CopyTo(myKeys, 0)
Console.WriteLine(" INDEX KEY VALUE")
Dim i As Integer
For i = 0 To myCol.Count - 1
Console.WriteLine(" {0,-5} {1,-25} {2}", i, myKeys(i), myCol(myKeys(i)))
Next i
Console.WriteLine()
End Sub
End Class
'This code produces the following output.
'
'Displays the elements using For Each:
' KEY VALUE
' red rojo
' blue azul
' green verde
'
'Displays the elements using the IEnumerator:
' KEY VALUE
' red rojo
' blue azul
' green verde
'
'Displays the elements using the Keys, Values, Count, and Item properties:
' INDEX KEY VALUE
' 0 red rojo
' 1 blue azul
' 2 green verde
설명
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) 작업입니다.