英語で読む

次の方法で共有


OrderedDictionary クラス

定義

キーまたはインデックスからアクセスできるキーと値のペアのコレクションを表します。

public class OrderedDictionary : System.Collections.IDictionary, System.Collections.Specialized.IOrderedDictionary
public class OrderedDictionary : System.Collections.IDictionary, System.Collections.Specialized.IOrderedDictionary, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
[System.Serializable]
public class OrderedDictionary : System.Collections.IDictionary, System.Collections.Specialized.IOrderedDictionary, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
public class OrderedDictionary : System.Collections.Specialized.IOrderedDictionary, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
継承
OrderedDictionary
派生
属性
実装

次のコード例では、コレクションのOrderedDictionary作成、作成、変更、および の内容OrderedDictionaryを表示する 2 つの手法を示します。1 つは および Values プロパティを使用し、もう 1 つは メソッドを使用KeysしてGetEnumerator列挙子を作成します。

// The following code example enumerates the elements of a OrderedDictionary.
using System;
using System.Collections;
using System.Collections.Specialized;

public class OrderedDictionarySample
{
    public static void Main()
    {

        // Creates and initializes a OrderedDictionary.
        OrderedDictionary myOrderedDictionary = new OrderedDictionary();
        myOrderedDictionary.Add("testKey1", "testValue1");
        myOrderedDictionary.Add("testKey2", "testValue2");
        myOrderedDictionary.Add("keyToDelete", "valueToDelete");
        myOrderedDictionary.Add("testKey3", "testValue3");

        ICollection keyCollection = myOrderedDictionary.Keys;
        ICollection valueCollection = myOrderedDictionary.Values;

        // Display the contents using the key and value collections
        DisplayContents(keyCollection, valueCollection, myOrderedDictionary.Count);

        // Modifying the OrderedDictionary
        if (!myOrderedDictionary.IsReadOnly)
        {
            // Insert a new key to the beginning of the OrderedDictionary
            myOrderedDictionary.Insert(0, "insertedKey1", "insertedValue1");

            // Modify the value of the entry with the key "testKey2"
            myOrderedDictionary["testKey2"] = "modifiedValue";

            // Remove the last entry from the OrderedDictionary: "testKey3"
            myOrderedDictionary.RemoveAt(myOrderedDictionary.Count - 1);

            // Remove the "keyToDelete" entry, if it exists
            if (myOrderedDictionary.Contains("keyToDelete"))
            {
                myOrderedDictionary.Remove("keyToDelete");
            }
        }

        Console.WriteLine(
            "{0}Displaying the entries of a modified OrderedDictionary.",
            Environment.NewLine);
        DisplayContents(keyCollection, valueCollection, myOrderedDictionary.Count);

        // Clear the OrderedDictionary and add new values
        myOrderedDictionary.Clear();
        myOrderedDictionary.Add("newKey1", "newValue1");
        myOrderedDictionary.Add("newKey2", "newValue2");
        myOrderedDictionary.Add("newKey3", "newValue3");

        // Display the contents of the "new" Dictionary using an enumerator
        IDictionaryEnumerator myEnumerator =
            myOrderedDictionary.GetEnumerator();

        Console.WriteLine(
            "{0}Displaying the entries of a \"new\" OrderedDictionary.",
            Environment.NewLine);

        DisplayEnumerator(myEnumerator);
    }

    // Displays the contents of the OrderedDictionary from its keys and values
    public static void DisplayContents(
        ICollection keyCollection, ICollection valueCollection, int dictionarySize)
    {
        String[] myKeys = new String[dictionarySize];
        String[] myValues = new String[dictionarySize];
        keyCollection.CopyTo(myKeys, 0);
        valueCollection.CopyTo(myValues, 0);

        // Displays the contents of the OrderedDictionary
        Console.WriteLine("   INDEX KEY                       VALUE");
        for (int i = 0; i < dictionarySize; i++)
        {
            Console.WriteLine("   {0,-5} {1,-25} {2}",
                i, myKeys[i], myValues[i]);
        }
        Console.WriteLine();
    }

    // Displays the contents of the OrderedDictionary using its enumerator
    public static void DisplayEnumerator(IDictionaryEnumerator myEnumerator)
    {
        Console.WriteLine("   KEY                       VALUE");
        while (myEnumerator.MoveNext())
        {
            Console.WriteLine("   {0,-25} {1}",
                myEnumerator.Key, myEnumerator.Value);
        }
    }
}

/*
This code produces the following output.

   INDEX KEY                       VALUE
   0     testKey1                  testValue1
   1     testKey2                  testValue2
   2     keyToDelete               valueToDelete
   3     testKey3                  testValue3


Displaying the entries of a modified OrderedDictionary.
   INDEX KEY                       VALUE
   0     insertedKey1              insertedValue1
   1     testKey1                  testValue1
   2     testKey2                  modifiedValue


Displaying the entries of a "new" OrderedDictionary.
   KEY                       VALUE
   newKey1                   newValue1
   newKey2                   newValue2
   newKey3                   newValue3

*/

注釈

各要素は、オブジェクトに格納されているキーと値のペアです DictionaryEntry 。 キーを に nullすることはできませんが、値は にできます。

クラスの要素とは異なり、 の OrderedDictionary 要素 SortedDictionary<TKey,TValue> はキーによって並べ替えられません。 要素には、キーまたはインデックスを使用してアクセスできます。

foreach C# 言語の ステートメント (For Each Visual Basic では ) は、コレクション内の各要素の型であるオブジェクトを返します。 コレクションの各要素はキーと値の OrderedDictionary ペアであるため、要素の型はキーの型または値の型ではありません。 代わりに、要素の型は です DictionaryEntry。 次のコードは、C#、Visual Basic、C++ の構文を示しています。

foreach (DictionaryEntry de in myOrderedDictionary)
{
    //...
}

ステートメントは foreach 列挙子を囲むラッパーであり、コレクションの書き込みではなく、コレクションからの読み取りのみを許可します。

コンストラクター

OrderedDictionary()

OrderedDictionary クラスの新しいインスタンスを初期化します。

OrderedDictionary(IEqualityComparer)

比較子を指定して、OrderedDictionary クラスの新しいインスタンスを初期化します。

OrderedDictionary(Int32)

指定した初期容量を使用して、OrderedDictionary クラスの新しいインスタンスを初期化します。

OrderedDictionary(Int32, IEqualityComparer)

指定した初期容量および比較子を使用して、OrderedDictionary クラスの新しいインスタンスを初期化します。

OrderedDictionary(SerializationInfo, StreamingContext)
古い.

指定した SerializationInfo オブジェクトと StreamingContext オブジェクトを使用してシリアル化できる、OrderedDictionary クラスの新しいインスタンスを初期化します。

プロパティ

Count

OrderedDictionary コレクションに格納されているキー/値ペアの数を取得します。

IsReadOnly

OrderedDictionary コレクションが読み取り専用かどうかを示す値を取得します。

Item[Int32]

指定したインデックス位置にある値を取得または設定します。

Item[Object]

指定したキーの値を取得または設定します。

Keys

OrderedDictionary コレクションのキーを保持している ICollection オブジェクトを取得します。

Values

OrderedDictionary コレクションの値を保持している ICollection オブジェクトを取得します。

メソッド

Add(Object, Object)

指定したキーおよび値を持つエントリを、使用できる最小のインデックスを持つ OrderedDictionary コレクションに追加します。

AsReadOnly()

現在の OrderedDictionary コレクションの読み取り専用のコピーを返します。

Clear()

OrderedDictionary コレクションからすべての要素を削除します。

Contains(Object)

OrderedDictionary コレクションに特定のキーが格納されているかどうかを判断します。

CopyTo(Array, Int32)

1 次元の Array オブジェクトの指定したインデックスに OrderedDictionary の要素をコピーします。

Equals(Object)

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

(継承元 Object)
GetEnumerator()

IDictionaryEnumerator コレクションを繰り返し処理する OrderedDictionary オブジェクトを返します。

GetHashCode()

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

(継承元 Object)
GetObjectData(SerializationInfo, StreamingContext)
古い.

ISerializable インターフェイスを実装し、OrderedDictionary コレクションをシリアル化するために必要なデータを返します。

GetType()

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

(継承元 Object)
Insert(Int32, Object, Object)

OrderedDictionary コレクションの指定したインデックス位置に、指定したキーと値を持つ新しいエントリを挿入します。

MemberwiseClone()

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

(継承元 Object)
OnDeserialization(Object)

ISerializable インターフェイスを実装し、逆シリアル化が完了したときに逆シリアル化イベントによってコールバックされます。

Remove(Object)

指定したキーを持つエントリを OrderedDictionary コレクションから削除します。

RemoveAt(Int32)

指定したインデックス位置にあるエントリを OrderedDictionary コレクションから削除します。

ToString()

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

(継承元 Object)

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

ICollection.IsSynchronized

OrderedDictionary オブジェクトへのアクセスが同期されている (スレッド セーフである) かどうかを示す値を取得します。

ICollection.SyncRoot

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

IDeserializationCallback.OnDeserialization(Object)

ISerializable インターフェイスを実装し、逆シリアル化が完了したときに逆シリアル化イベントによってコールバックされます。

IDictionary.IsFixedSize

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

IEnumerable.GetEnumerator()

IDictionaryEnumerator コレクションを繰り返し処理する OrderedDictionary オブジェクトを返します。

拡張メソッド

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 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