英語で読む

次の方法で共有


ListDictionary クラス

定義

シングルリンク リストを使用して IDictionary を実装します。 おおむね 10 個未満の項目を格納するコレクションでの使用をお勧めします。

C#
public class ListDictionary : System.Collections.IDictionary
C#
[System.Serializable]
public class ListDictionary : System.Collections.IDictionary
継承
ListDictionary
属性
実装

次のコード例では、 のプロパティとメソッドをいくつか示します ListDictionary

C#
using System;
using System.Collections;
using System.Collections.Specialized;

public class SamplesListDictionary  {

   public static void Main()  {

      // Creates and initializes a new ListDictionary.
      ListDictionary myCol = new ListDictionary();
      myCol.Add( "Braeburn Apples", "1.49" );
      myCol.Add( "Fuji Apples", "1.29" );
      myCol.Add( "Gala Apples", "1.49" );
      myCol.Add( "Golden Delicious Apples", "1.29" );
      myCol.Add( "Granny Smith Apples", "0.89" );
      myCol.Add( "Red Delicious Apples", "0.99" );

      // 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 IDictionaryEnumerator:" );
      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 );

      // Copies the ListDictionary to an array with DictionaryEntry elements.
      DictionaryEntry[] myArr = new DictionaryEntry[myCol.Count];
      myCol.CopyTo( myArr, 0 );

      // Displays the values in the array.
      Console.WriteLine( "Displays the elements in the array:" );
      Console.WriteLine( "   KEY                       VALUE" );
      for ( int i = 0; i < myArr.Length; i++ )
         Console.WriteLine( "   {0,-25} {1}", myArr[i].Key, myArr[i].Value );
      Console.WriteLine();

      // Searches for a key.
      if ( myCol.Contains( "Kiwis" ) )
         Console.WriteLine( "The collection contains the key \"Kiwis\"." );
      else
         Console.WriteLine( "The collection does not contain the key \"Kiwis\"." );
      Console.WriteLine();

      // Deletes a key.
      myCol.Remove( "Plums" );
      Console.WriteLine( "The collection contains the following elements after removing \"Plums\":" );
      PrintKeysAndValues1( myCol );

      // Clears the entire collection.
      myCol.Clear();
      Console.WriteLine( "The collection contains the following elements after it is cleared:" );
      PrintKeysAndValues1( 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( IDictionary 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( IDictionary myCol )  {
      IDictionaryEnumerator myEnumerator = myCol.GetEnumerator();
      Console.WriteLine( "   KEY                       VALUE" );
      while ( myEnumerator.MoveNext() )
         Console.WriteLine( "   {0,-25} {1}", myEnumerator.Key, myEnumerator.Value );
      Console.WriteLine();
   }

   // Uses the Keys, Values, Count, and Item properties.
   public static void PrintKeysAndValues3( ListDictionary 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 output similar to the following.
Note that because a dictionary is implemented for fast keyed access the order
of the items in the dictionary are not gauranteed and, as a result, should not
be depended on.

Displays the elements using foreach:
   KEY                       VALUE
   Braeburn Apples           1.49
   Fuji Apples               1.29
   Gala Apples               1.49
   Golden Delicious Apples   1.29
   Granny Smith Apples       0.89
   Red Delicious Apples      0.99

Displays the elements using the IDictionaryEnumerator:
   KEY                       VALUE
   Braeburn Apples           1.49
   Fuji Apples               1.29
   Gala Apples               1.49
   Golden Delicious Apples   1.29
   Granny Smith Apples       0.89
   Red Delicious Apples      0.99

Displays the elements using the Keys, Values, Count, and Item properties:
   INDEX KEY                       VALUE
   0     Braeburn Apples           1.49
   1     Fuji Apples               1.29
   2     Gala Apples               1.49
   3     Golden Delicious Apples   1.29
   4     Granny Smith Apples       0.89
   5     Red Delicious Apples      0.99

Displays the elements in the array:
   KEY                       VALUE
   Braeburn Apples           1.49
   Fuji Apples               1.29
   Gala Apples               1.49
   Golden Delicious Apples   1.29
   Granny Smith Apples       0.89
   Red Delicious Apples      0.99

The collection does not contain the key "Kiwis".

The collection contains the following elements after removing "Plums":
   KEY                       VALUE
   Braeburn Apples           1.49
   Fuji Apples               1.29
   Gala Apples               1.49
   Golden Delicious Apples   1.29
   Granny Smith Apples       0.89
   Red Delicious Apples      0.99

The collection contains the following elements after it is cleared:
   KEY                       VALUE


*/

注釈

これは、1 つのリンクされたリストを使用する簡単な実装 IDictionary です。 要素の数が 10 以下の場合は、 より Hashtable 小さく高速です。 これは、多数の要素のパフォーマンスが重要な場合には使用しないでください。

内の ListDictionary 項目は、保証された順序ではありません。コードは現在の順序に依存しないでください。 ListDictionaryは、迅速なキー取得のために実装されます。項目の実際の内部順序は実装に依存し、将来のバージョンの製品で変更される可能性があります。

、 などのItem[]RemoveContainsAddメンバーは O(n) 操作です。ここでn、 は Countです。

キーを に nullすることはできませんが、値を指定できます。

C# 言語の ステートメント (for each Visual Basic の場合) はforeach、コレクション内の要素の型のオブジェクトを返します。 の ListDictionary 各要素はキーと値のペアであるため、要素の型はキーの型や値の型ではありません。 代わりに、要素の型は です DictionaryEntry。 次に例を示します。

C#
foreach (DictionaryEntry de in myListDictionary)
{
    //...
}

ステートメントは foreach 列挙子のラッパーであり、コレクションからの読み取りのみが許可され、コレクションへの書き込みは許可されません。

コンストラクター

ListDictionary()

既定の比較子を使用して空の ListDictionary を作成します。

ListDictionary(IComparer)

指定した比較子を使用して空の ListDictionary を作成します。

プロパティ

Count

ListDictionary に格納されているキー/値ペアの数を取得します。

IsFixedSize

ListDictionary が固定サイズかどうかを示す値を取得します。

IsReadOnly

ListDictionary が読み取り専用かどうかを示す値を取得します。

IsSynchronized

ListDictionary が同期されている (スレッド セーフである) かどうかを示す値を取得します。

Item[Object]

指定されたキーに関連付けられている値を取得または設定します。

Keys

ListDictionary 内のキーを格納している ICollection を取得します。

SyncRoot

ListDictionary へのアクセスを同期するために使用できるオブジェクトを取得します。

Values

ICollection 内の値を格納している ListDictionary を取得します。

メソッド

Add(Object, Object)

指定したキーおよび値を持つエントリを ListDictionary に追加します。

Clear()

ListDictionary からすべてのエントリを削除します。

Contains(Object)

ListDictionary に特定のキーが格納されているかどうかを判断します。

CopyTo(Array, Int32)

1 次元の Array インスタンスの指定したインデックスに ListDictionary のエントリをコピーします。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetEnumerator()

ListDictionary を反復処理する IDictionaryEnumerator を返します。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
Remove(Object)

指定したキーを持つエントリを ListDictionary から削除します。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

IEnumerable.GetEnumerator()

ListDictionary を反復処理する IEnumerator を返します。

拡張メソッド

Cast<TResult>(IEnumerable)

IEnumerable の要素を、指定した型にキャストします。

OfType<TResult>(IEnumerable)

指定された型に基づいて IEnumerable の要素をフィルター処理します。

AsParallel(IEnumerable)

クエリの並列化を有効にします。

AsQueryable(IEnumerable)

IEnumerableIQueryable に変換します。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

スレッド セーフ

パブリック静的 (Visual Basic ではShared) なこの型のメンバーはスレッド セーフです インスタンス メンバーの場合は、スレッド セーフであるとは限りません。

この実装では、 の同期 (スレッド セーフ) ラッパーListDictionaryは提供されませんが、派生クラスでは、 プロパティを使用して独自の同期バージョンの をListDictionarySyncRoot作成できます。

コレクションの列挙処理は、本質的にスレッドセーフな処理ではありません。 コレクションが同期されていても、他のスレッドがコレクションを変更する場合があり、このときは列挙子から例外がスローされます。 列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチします。

こちらもご覧ください