SortedList.System.Collections.IDictionary.GetEnumerator メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > SortedList.System.Collections.IDictionary.GetEnumerator メソッドの意味・解説 

SortedList.System.Collections.IDictionary.GetEnumerator メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

IDictionary の IDictionaryEnumerator を返します

名前空間: System.Collections.Generic
アセンブリ: System (system.dll 内)
構文構文

Private Function System.Collections.IDictionary.GetEnumerator
 As IDictionaryEnumerator Implements IDictionary.GetEnumerator
Dim instance As SortedList(Of
 TKey, TValue)
Dim returnValue As IDictionaryEnumerator

returnValue = CType(instance, IDictionary).GetEnumerator
IDictionaryEnumerator IDictionary.GetEnumerator ()
private:
virtual IDictionaryEnumerator^ System.Collections.IDictionary.GetEnumerator () sealed
 = IDictionary::GetEnumerator

戻り値
IDictionaryIDictionaryEnumerator

解説解説

C# 言語foreach ステートメント (C++ の場合for eachVisual Basic の場合For Each) を使用することで列挙子の複雑さ回避できます。したがって列挙子を直接操作するではなくforeach使用お勧めます。

列挙子を使用すると、コレクション内のデータ読み取ることができますが、基になるコレクション変更することはできません。

初期状態では、列挙子はコレクション最初要素前に位置してます。Reset実行した場合も、列挙子はこの位置に戻されます。この位置では、Entry未定義です。したがってEntry の値を読み取る前に、MoveNext を呼び出してコレクション最初要素列挙子を進める必要があります

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

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

コレクション変更されない限り列挙子は有効なままです。要素追加変更削除などの変更コレクションに対して実行されると、列挙子は回復不可能な無効状態になり、動作未定義になります

列挙子はコレクションへの排他アクセス権持たないため、コレクション列挙処理は、本質的にスレッド セーフな処理ではありません。すべての列挙処理が終わるまでコレクションロックすることにより、列挙処理でのスレッド セーフ確保できますコレクション対し複数スレッドアクセスして読み取り書き込みを行うことができるようにするには、独自に同期化実装する必要があります

System.Collections.Generic のコレクション既定実装同期されません。

このメソッドは O(1) 操作です。

使用例使用例

foreach ステートメント (Visual Basic の場合For EachC++ の場合for each) を使用して並べ替えられたリストキー/値ペア列挙する方法次のコード例示します。このステートメント使用すると、列挙子を使用していることが隠されます。特に、System.Collections.IDictionary インターフェイス列挙子が KeyValuePair オブジェクトではなく DictionaryEntry オブジェクト返すことに注意してください

次のコード例は、System.Collections.IDictionary.Add メソッドトピック取り上げている例および出力一部分です。

Imports System
Imports System.Collections
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main()
 

        ' Create a new sorted list of strings, with string keys,
        ' and access it using the IDictionary interface.
        '
        Dim openWith As IDictionary = _
            New sortedList(Of String,
 String)
        
        ' Add some elements to the sorted list. There are no 
        ' duplicate keys, but some of the values are duplicates.
        ' IDictionary.Add throws an exception if incorrect types
        ' are supplied for key or value.
        openWith.Add("txt", "notepad.exe")
        openWith.Add("bmp", "paint.exe")
        openWith.Add("dib", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")
<br /><span space="preserve">...</span><br
 />        ' When you use foreach to enumerate sorted list elements
        ' with the IDictionary interface, the elements are retrieved
        ' as DictionaryEntry objects instead of KeyValuePair objects.
        Console.WriteLine()
        For Each de As DictionaryEntry
 In openWith
            Console.WriteLine("Key = {0}, Value = {1}",
 _
                de.Key, de.Value)
        Next 
<br /><span space="preserve">...</span><br
 />
    End Sub

End Class
using System;
using System.Collections;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new sorted list of strings, with string keys,
        // and access it using the IDictionary interface.
        //
        IDictionary openWith = new SortedList<string,
 string>();

        // Add some elements to the sorted list. There are no 
        // duplicate keys, but some of the values are duplicates.
        // IDictionary.Add throws an exception if incorrect types
        // are supplied for key or value.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("dib", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");
<br /><span space="preserve">...</span><br /> 
       // When you use foreach to enumerate sorted list elements
        // with the IDictionary interface, the elements are retrieved
        // as DictionaryEntry objects instead of KeyValuePair objects.
        Console.WriteLine();
        foreach( DictionaryEntry de in openWith
 )
        {
            Console.WriteLine("Key = {0}, Value = {1}", 
                de.Key, de.Value);
        }
<br /><span space="preserve">...</span><br /> 
   }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SortedList ジェネリック クラス
SortedList メンバ
System.Collections.Generic 名前空間
SortedList.GetEnumerator メソッド
IDictionaryEnumerator インターフェイス



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

辞書ショートカット

すべての辞書の索引

SortedList.System.Collections.IDictionary.GetEnumerator メソッドのお隣キーワード
検索ランキング

   

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



SortedList.System.Collections.IDictionary.GetEnumerator メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS