IDictionaryEnumerator 인터페이스
제네릭이 아닌 사전의 요소를 열거합니다.
네임스페이스: System.Collections
어셈블리: mscorlib(mscorlib.dll)
구문
‘선언
<ComVisibleAttribute(True)> _
Public Interface IDictionaryEnumerator
Inherits IEnumerator
‘사용 방법
Dim instance As IDictionaryEnumerator
[ComVisibleAttribute(true)]
public interface IDictionaryEnumerator : IEnumerator
[ComVisibleAttribute(true)]
public interface class IDictionaryEnumerator : IEnumerator
/** @attribute ComVisibleAttribute(true) */
public interface IDictionaryEnumerator extends IEnumerator
ComVisibleAttribute(true)
public interface IDictionaryEnumerator extends IEnumerator
설명
C# 언어의 foreach 문(Visual Basic에서는 for each)은 열거자를 덜 복잡하게 표시합니다. 따라서 열거자를 직접 조작하는 것보다는 foreach를 사용하는 것이 좋습니다.
열거자는 컬렉션에 있는 데이터를 읽는 데 사용할 수 있지만 내부 컬렉션을 수정하는 데에는 사용할 수 없습니다.
처음에는 열거자가 컬렉션의 첫 번째 요소 앞에 위치합니다. Reset 메서드는 또한 열거자를 이 위치로 다시 가져옵니다. 이 위치에서 Current 속성을 호출하면 예외가 throw됩니다. 따라서 Current의 값을 읽기 전에 MoveNext 메서드를 호출하여 열거자를 해당 컬렉션의 첫 번째 요소로 보내야 합니다.
Current에서는 MoveNext 또는 Reset이 호출될 때까지 동일한 개체를 반환합니다. MoveNext는 Current를 다음 요소로 설정합니다.
MoveNext가 컬렉션의 끝을 지나게 되면 열거자는 컬렉션의 마지막 요소 뒤에 배치되고, MoveNext는 false를 반환합니다. 열거자가 이 위치에 있는 경우 MoveNext에 대한 후속 호출 또한 false를 반환합니다. MoveNext에 대한 마지막 호출에서 false가 반환된 경우 Current를 호출하면 예외가 throw됩니다. Current를 해당 컬렉션의 첫 번째 요소로 다시 설정하려면 Reset을 호출한 다음 MoveNext를 호출하면 됩니다.
열거자는 컬렉션이 변경되지 않은 상태로 유지되는 한 유효합니다. 그러나 요소를 추가, 수정, 삭제하는 등 컬렉션을 변경하면 열거자는 더 이상 유효하지 않으며(복구할 수 없음) 다음에 MoveNext 또는 Reset을 호출하면 InvalidOperationException이 throw됩니다. MoveNext와 Current 사이에서 컬렉션이 수정되면 Current는 열거자가 이미 유효하지 않더라도 자신이 설정한 요소를 반환합니다.
열거자는 컬렉션에 단독으로 액세스할 수 있는 권한이 없으므로 컬렉션을 열거하는 프로시저는 기본적으로 스레드로부터 안전하지 않습니다. 컬렉션이 동기화되어 있을 때 다른 스레드에서 해당 컬렉션을 수정할 수 있으므로 이렇게 되면 열거자에서 예외가 throw됩니다. 열거하는 동안 스레드로부터 안전을 보장하려면 전체 열거를 수행하는 동안 컬렉션을 잠그거나 다른 스레드에서 변경된 내용으로 인해 발생한 예외를 catch하면 됩니다.
구현자 참고 사항 IEnumerator에서 상속되는 Current 속성은 Entry 속성의 반환 값과 비슷한 boxed DictionaryEntry인 Object를 반환합니다.
예제
이 코드 예제에서는 IDictionaryEnumerator 인터페이스를 구현하는 사전 열거자를 정의하는 방법을 보여 줍니다.
Private Class SimpleDictionaryEnumerator
Implements IDictionaryEnumerator
' A copy of the SimpleDictionary object's key/value pairs.
Dim items() As DictionaryEntry
Dim index As Integer = -1
Public Sub New(ByVal sd As SimpleDictionary)
' Make a copy of the dictionary entries currently in the SimpleDictionary object.
items = New DictionaryEntry(sd.Count - 1) {}
Array.Copy(sd.items, 0, items, 0, sd.Count)
End Sub
' Return the current item.
Public ReadOnly Property Current() As Object Implements IDictionaryEnumerator.Current
Get
ValidateIndex()
Return items(index)
End Get
End Property
' Return the current dictionary entry.
Public ReadOnly Property Entry() As DictionaryEntry Implements IDictionaryEnumerator.Entry
Get
Return Current
End Get
End Property
' Return the key of the current item.
Public ReadOnly Property Key() As Object Implements IDictionaryEnumerator.Key
Get
ValidateIndex()
Return items(index).Key
End Get
End Property
' Return the value of the current item.
Public ReadOnly Property Value() As Object Implements IDictionaryEnumerator.Value
Get
ValidateIndex()
Return items(index).Value
End Get
End Property
' Advance to the next item.
Public Function MoveNext() As Boolean Implements IDictionaryEnumerator.MoveNext
If index < items.Length - 1 Then
index = index + 1
Return True
End If
Return False
End Function
' Validate the enumeration index and throw an exception if the index is out of range.
Private Sub ValidateIndex()
If index < 0 Or index >= items.Length Then
Throw New InvalidOperationException("Enumerator is before or after the collection.")
End If
End Sub
' Reset the index to restart the enumeration.
Public Sub Reset() Implements IDictionaryEnumerator.Reset
index = -1
End Sub
End Class
Public Function GetEnumerator() As IDictionaryEnumerator Implements IDictionary.GetEnumerator
'Construct and return an enumerator.
Return New SimpleDictionaryEnumerator(Me)
End Function
' ICollection Members
Public ReadOnly Property IsSynchronized() As Boolean Implements IDictionary.IsSynchronized
Get
Return False
End Get
End Property
Public ReadOnly Property SyncRoot() As Object Implements IDictionary.SyncRoot
Get
Throw New NotImplementedException()
End Get
End Property
Public ReadOnly Property Count() As Integer Implements IDictionary.Count
Get
Return ItemsInUse
End Get
End Property
Public Sub CopyTo(ByVal array As Array, ByVal index As Integer) Implements IDictionary.CopyTo
Throw New NotImplementedException()
End Sub
' IEnumerable Members
Public Function GetEnumerator1() As IEnumerator Implements IEnumerable.GetEnumerator
' Construct and return an enumerator.
Return Me.GetEnumerator()
End Function
End Class
private class SimpleDictionaryEnumerator : IDictionaryEnumerator
{
// A copy of the SimpleDictionary object's key/value pairs.
DictionaryEntry[] items;
Int32 index = -1;
public SimpleDictionaryEnumerator(SimpleDictionary sd)
{
// Make a copy of the dictionary entries currently in the SimpleDictionary object.
items = new DictionaryEntry[sd.Count];
Array.Copy(sd.items, 0, items, 0, sd.Count);
}
// Return the current item.
public Object Current { get { ValidateIndex(); return items[index]; } }
// Return the current dictionary entry.
public DictionaryEntry Entry
{
get { return (DictionaryEntry) Current; }
}
// Return the key of the current item.
public Object Key { get { ValidateIndex(); return items[index].Key; } }
// Return the value of the current item.
public Object Value { get { ValidateIndex(); return items[index].Value; } }
// Advance to the next item.
public Boolean MoveNext()
{
if (index < items.Length - 1) { index++; return true; }
return false;
}
// Validate the enumeration index and throw an exception if the index is out of range.
private void ValidateIndex()
{
if (index < 0 || index >= items.Length)
throw new InvalidOperationException("Enumerator is before or after the collection.");
}
// Reset the index to restart the enumeration.
public void Reset()
{
index = -1;
}
}
public IDictionaryEnumerator GetEnumerator()
{
// Construct and return an enumerator.
return new SimpleDictionaryEnumerator(this);
}
#endregion
#region ICollection Members
public bool IsSynchronized { get { return false; } }
public object SyncRoot { get { throw new NotImplementedException(); } }
public int Count { get { return ItemsInUse; } }
public void CopyTo(Array array, int index) { throw new NotImplementedException(); }
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
// Construct and return an enumerator.
return ((IDictionary)this).GetEnumerator();
}
#endregion
}
private:
ref class SimpleDictionaryEnumerator : public IDictionaryEnumerator
{
// A copy of the SimpleDictionary object's key/value pairs.
private:
array<DictionaryEntry^>^ items;
private:
int index;
public:
SimpleDictionaryEnumerator(SimpleDictionary^ sd)
{
// Make a copy of the dictionary entries currently in the
// SimpleDictionary object.
items = gcnew array<DictionaryEntry^>(sd->Count);
Array::Copy(sd->items, 0, items, 0, sd->Count);
index = -1;
}
// Return the current item.
public:
virtual property Object^ Current
{
Object^ get()
{
ValidateIndex();
return items[index];
}
}
// Return the current dictionary entry.
public:
virtual property DictionaryEntry Entry
{
DictionaryEntry get()
{
return (DictionaryEntry) Current;
}
}
// Return the key of the current item.
public:
virtual property Object^ Key
{
Object^ get()
{
ValidateIndex();
return items[index]->Key;
}
}
// Return the value of the current item.
public:
virtual property Object^ Value
{
Object^ get()
{
ValidateIndex();
return items[index]->Value;
}
}
// Advance to the next item.
public:
virtual bool MoveNext()
{
if (index < items->Length - 1)
{
index++;
return true;
}
return false;
}
// Validate the enumeration index and throw an exception if
// the index is out of range.
private:
void ValidateIndex()
{
if (index < 0 || index >= items->Length)
{
throw gcnew InvalidOperationException
("Enumerator is before or after the collection.");
}
}
// Reset the index to restart the enumeration.
public:
virtual void Reset()
{
index = -1;
}
};
public:
virtual IDictionaryEnumerator^ GetEnumerator()
{
// Construct and return an enumerator.
return gcnew SimpleDictionaryEnumerator(this);
}
#pragma endregion
#pragma region ICollection Members
public:
virtual property bool IsSynchronized
{
bool get()
{
return false;
}
}
public:
virtual property Object^ SyncRoot
{
Object^ get()
{
throw gcnew NotImplementedException();
}
}
public:
virtual property int Count
{
int get()
{
return itemsInUse;
}
}
public:
virtual void CopyTo(Array^ array, int index)
{
throw gcnew NotImplementedException();
}
#pragma endregion
#pragma region IEnumerable Members
virtual IEnumerator^ IEnumerable_GetEnumerator()
= IEnumerable::GetEnumerator
{
// Construct and return an enumerator.
return ((IDictionary^)this)->GetEnumerator();
}
#pragma endregion
};
플랫폼
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에서 지원
참고 항목
참조
IDictionaryEnumerator 멤버
System.Collections 네임스페이스
IDictionary 인터페이스
IEnumerator
System.Collections.Generic.IEnumerator