IDictionaryEnumeratorとは? わかりやすく解説

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 プロパティ呼び出すと、例外スローさます。たがってCurrent の値を読み取る前に、MoveNext メソッド呼び出してコレクション最初要素列挙子を進める必要があります

Current は、MoveNext または Reset呼び出されるまでは同じオブジェクト返しますMoveNext は、Current次の要素設定します

MoveNextコレクション末尾を過ぎると、列挙子はコレクション最後要素後ろ配置されMoveNextfalse返します列挙子がこの位置にある場合以降MoveNext呼び出してfalse返されます。MoveNext への最後呼び出しfalse返され場合に、Current呼び出すと例外スローさます。Currentコレクション最初要素に再び設定するには、Reset呼び出してから、MoveNext呼び出します。

列挙子は、コレクション変更されない限り有効です。要素追加変更削除などの変更コレクションに対して実行されると、列挙子は回復不可能な無効状態になり、次に MoveNext または Reset呼び出すと InvalidOperationException がスローさます。MoveNextCurrent の間でコレクション変更すると、列挙子が無効になっている場合でも、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
};
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
IDictionaryEnumerator メンバ
System.Collections 名前空間
IDictionary インターフェイス
IEnumerator
System.Collections.Generic.IEnumerator

IDictionaryEnumerator プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ Value 現在のディクショナリ エントリの値を取得します
参照参照

関連項目

IDictionaryEnumerator インターフェイス
System.Collections 名前空間
IDictionary インターフェイス
IEnumerator
System.Collections.Generic.IEnumerator

IDictionaryEnumerator メンバ

ジェネリック ディクショナリの要素列挙します

IDictionaryEnumerator データ型公開されるメンバを以下の表に示します


パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ Value 現在のディクショナリ エントリの値を取得します
参照参照

関連項目

IDictionaryEnumerator インターフェイス
System.Collections 名前空間
IDictionary インターフェイス
IEnumerator
System.Collections.Generic.IEnumerator



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「IDictionaryEnumerator」の関連用語

IDictionaryEnumeratorのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



IDictionaryEnumeratorのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS