IDictionaryEnumerator インターフェイス
アセンブリ: mscorlib (mscorlib.dll 内)


C# 言語の foreach ステートメント (Visual Basic の場合は for each) を使用することで列挙子の複雑さを回避できます。したがって、列挙子を直接操作するのではなく、foreach の使用をお勧めします。
列挙子を使用すると、コレクション内のデータを読み取ることができますが、基になるコレクションを変更することはできません。
最初に、列挙子はコレクションの最初の要素の前に配置されます。また、Reset メソッドは、列挙子を最初の位置に戻します。この位置で Current プロパティを呼び出すと、例外がスローされます。したがって、Current の値を読み取る前に、MoveNext メソッドを呼び出して、コレクションの最初の要素に列挙子を進める必要があります。
Current は、MoveNext または Reset が呼び出されるまでは同じオブジェクトを返します。MoveNext は、Current を次の要素に設定します。
MoveNext がコレクションの末尾を過ぎると、列挙子はコレクションの最後の要素の後ろに配置され、MoveNext は false を返します。列挙子がこの位置にある場合、以降、MoveNext を呼び出しても false が返されます。MoveNext への最後の呼び出しで false が返された場合に、Current を呼び出すと例外がスローされます。Current をコレクションの最初の要素に再び設定するには、Reset を呼び出してから、MoveNext を呼び出します。
列挙子は、コレクションが変更されない限り有効です。要素の追加、変更、削除などの変更がコレクションに対して実行されると、列挙子は回復不可能な無効状態になり、次に MoveNext または Reset を呼び出すと InvalidOperationException がスローされます。MoveNext と Current の間でコレクションを変更すると、列挙子が無効になっている場合でも、Current が設定先の要素を返します。
列挙子はコレクションへの排他アクセス権を持たないため、コレクションの列挙処理は、本質的にはスレッド セーフな処理ではありません。コレクションが同期されている場合でも、他のスレッドがそのコレクションを変更する可能性はあり、そのような状況が発生すると列挙子は例外をスローします。列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチします。
実装時の注意 IEnumerator から継承された Current プロパティは、Object を返します。これは、Entry プロパティの戻り値と同様に、ボックス化された DictionaryEntry です。
このコード例では、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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


IDictionaryEnumerator メンバ
System.Collections 名前空間
IDictionary インターフェイス
IEnumerator
System.Collections.Generic.IEnumerator
IDictionaryEnumerator プロパティ


関連項目
IDictionaryEnumerator インターフェイスSystem.Collections 名前空間
IDictionary インターフェイス
IEnumerator
System.Collections.Generic.IEnumerator
IDictionaryEnumerator メンバ
- IDictionaryEnumeratorのページへのリンク