OrderedDictionary OrderedDictionary OrderedDictionary OrderedDictionary Class

定義

キーまたはインデックスからアクセスできるキーと値のペアのコレクションを表します。 Represents a collection of key/value pairs that are accessible by the key or index.

public ref class OrderedDictionary : System::Collections::IDictionary, System::Collections::Specialized::IOrderedDictionary, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
[Serializable]
public class OrderedDictionary : System.Collections.IDictionary, System.Collections.Specialized.IOrderedDictionary, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
type OrderedDictionary = class
    interface IOrderedDictionary
    interface ISerializable
    interface IDeserializationCallback
    interface IDictionary
    interface ICollection
    interface IEnumerable
Public Class OrderedDictionary
Implements IDeserializationCallback, IDictionary, IOrderedDictionary, ISerializable
継承
OrderedDictionaryOrderedDictionaryOrderedDictionaryOrderedDictionary
派生
属性
SerializableAttribute
実装

次のコード例は、作成、カタログの作成および変更を示して、OrderedDictionaryの内容を表示する 2 つの手法と同様に、コレクション、 OrderedDictionary: 1 つを使用して、KeysValuesプロパティと、その他列挙子を作成、GetEnumeratorメソッド。The following code example demonstrates the creation, population and modification of an OrderedDictionary collection, as well as two techniques to display the contents of the OrderedDictionary: one using the Keys and Values properties and the other creating an enumerator through the GetEnumerator method.

// The following code example enumerates the elements of a OrderedDictionary.
#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;

public ref class OrderedDictionarySample
{
public:
    static void Main()
    {
        // Creates and initializes a OrderedDictionary.
        OrderedDictionary^ myOrderedDictionary = gcnew 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);

        Console::ReadLine();
    }

    // Displays the contents of the OrderedDictionary from its keys and values
    static void DisplayContents(
        ICollection^ keyCollection, ICollection^ valueCollection, int dictionarySize)
    {
        array<String^>^ myKeys = gcnew array<String^>(dictionarySize);
        array<String^>^ myValues = gcnew array<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
    static void DisplayEnumerator(IDictionaryEnumerator^ myEnumerator)
    {
        Console::WriteLine("   KEY                       VALUE");
        while (myEnumerator->MoveNext())
        {
            Console::WriteLine("   {0,-25} {1}",
                myEnumerator->Key, myEnumerator->Value);
        }
    }
};

int main()
{
    OrderedDictionarySample::Main();
}

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

*/
// 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);

        Console.ReadLine();
    }

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

*/
' The following code example enumerates the elements of a OrderedDictionary.
Imports System
Imports System.Collections
Imports System.Collections.Specialized

Public Class OrderedDictionarySample

    Public Shared Sub Main()

        ' Creates and initializes a OrderedDictionary.
        Dim myOrderedDictionary As New OrderedDictionary()
        myOrderedDictionary.Add("testKey1", "testValue1")
        myOrderedDictionary.Add("testKey2", "testValue2")
        myOrderedDictionary.Add("keyToDelete", "valueToDelete")
        myOrderedDictionary.Add("testKey3", "testValue3")

        Dim keyCollection As ICollection = myOrderedDictionary.Keys
        Dim valueCollection As ICollection = myOrderedDictionary.Values

        ' Display the contents Imports the key and value collections
        DisplayContents( _
            keyCollection, valueCollection, myOrderedDictionary.Count)

        ' Modifying the OrderedDictionary
        If Not myOrderedDictionary.IsReadOnly Then

            ' 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")) Then
                myOrderedDictionary.Remove("keyToDelete")
            End If
        End If

        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 Imports an enumerator
        Dim myEnumerator As IDictionaryEnumerator = _
            myOrderedDictionary.GetEnumerator()

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

        DisplayEnumerator(myEnumerator)

        Console.ReadLine()
    End Sub

    ' Displays the contents of the OrderedDictionary from its keys and values
    Public Shared Sub DisplayContents( _
        ByVal keyCollection As ICollection, _
        ByVal valueCollection As ICollection, ByVal dictionarySize As Integer)

        Dim myKeys(dictionarySize) As [String]
        Dim myValues(dictionarySize) As [String]
        keyCollection.CopyTo(myKeys, 0)
        valueCollection.CopyTo(myValues, 0)

        ' Displays the contents of the OrderedDictionary
        Console.WriteLine("   INDEX KEY                       VALUE")
        Dim i As Integer
        For i = 0 To dictionarySize - 1
            Console.WriteLine("   {0,-5} {1,-25} {2}", _
                 i, myKeys(i), myValues(i))
        Next i
        Console.WriteLine()
    End Sub

    ' Displays the contents of the OrderedDictionary using its enumerator
    Public Shared Sub DisplayEnumerator( _
        ByVal myEnumerator As IDictionaryEnumerator)

        Console.WriteLine("   KEY                       VALUE")
        While myEnumerator.MoveNext()
            Console.WriteLine("   {0,-25} {1}", _
                myEnumerator.Key, myEnumerator.Value)
        End While
    End Sub
End Class

'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オブジェクト。Each element is a key/value pair stored in a DictionaryEntry object. キーにはnullが値を使用できます。A key cannot be null, but a value can be.

要素、OrderedDictionaryはの要素とは異なり、キーによって並べ替えられていない、SortedDictionary<TKey,TValue>クラス。The elements of an OrderedDictionary are not sorted by the key, unlike the elements of a SortedDictionary<TKey,TValue> class. 要素は、キーまたはインデックスのいずれかにアクセスすることができます。You can access elements either by the key or by the index.

foreach C# 言語のステートメント (For Each Visual basic)、コレクション内の各要素の型のオブジェクトを返します。The foreach statement of the C# language (For Each in Visual Basic) returns objects that are of the type of each element in the collection. 以降の各要素、OrderedDictionaryコレクションは、キー/値ペアを要素の型がないと、キーの型または値の型。Since each element of the OrderedDictionary collection is a key/value pair, the element type is not the type of the key or the type of the value. 要素の型は、代わりに、DictionaryEntryします。Instead, the element type is DictionaryEntry. 次のコードは、c#、Visual Basic および C++ の構文を示しています。The following code shows C#, Visual Basic and C++ syntax.

for each (DictionaryEntry de in myOrderedDictionary)
{
    //...
}
foreach (DictionaryEntry de in myOrderedDictionary)
{
    //...
}
For Each de As DictionaryEntry In myOrderedDictionary
    '...
Next de

foreachステートメントは、コレクションを記述しないからの読み取りのみを許可する列挙子のラッパーです。The foreach statement is a wrapper around the enumerator, which only allows reading from, not writing to, the collection.

コンストラクター

OrderedDictionary() OrderedDictionary() OrderedDictionary() OrderedDictionary()

OrderedDictionary クラスの新しいインスタンスを初期化します。 Initializes a new instance of the OrderedDictionary class.

OrderedDictionary(IEqualityComparer) OrderedDictionary(IEqualityComparer) OrderedDictionary(IEqualityComparer) OrderedDictionary(IEqualityComparer)

比較子を指定して、OrderedDictionary クラスの新しいインスタンスを初期化します。 Initializes a new instance of the OrderedDictionary class using the specified comparer.

OrderedDictionary(Int32) OrderedDictionary(Int32) OrderedDictionary(Int32) OrderedDictionary(Int32)

指定した初期容量を使用して、OrderedDictionary クラスの新しいインスタンスを初期化します。 Initializes a new instance of the OrderedDictionary class using the specified initial capacity.

OrderedDictionary(Int32, IEqualityComparer) OrderedDictionary(Int32, IEqualityComparer) OrderedDictionary(Int32, IEqualityComparer) OrderedDictionary(Int32, IEqualityComparer)

指定した初期容量および比較子を使用して、OrderedDictionary クラスの新しいインスタンスを初期化します。 Initializes a new instance of the OrderedDictionary class using the specified initial capacity and comparer.

OrderedDictionary(SerializationInfo, StreamingContext) OrderedDictionary(SerializationInfo, StreamingContext) OrderedDictionary(SerializationInfo, StreamingContext) OrderedDictionary(SerializationInfo, StreamingContext)

指定した SerializationInfo オブジェクトと StreamingContext オブジェクトを使用してシリアル化できる、OrderedDictionary クラスの新しいインスタンスを初期化します。 Initializes a new instance of the OrderedDictionary class that is serializable using the specified SerializationInfo and StreamingContext objects.

プロパティ

Count Count Count Count

OrderedDictionary コレクションに格納されているキー/値ペアの数を取得します。 Gets the number of key/values pairs contained in the OrderedDictionary collection.

IsReadOnly IsReadOnly IsReadOnly IsReadOnly

OrderedDictionary コレクションが読み取り専用かどうかを示す値を取得します。 Gets a value indicating whether the OrderedDictionary collection is read-only.

Item[Int32] Item[Int32] Item[Int32] Item[Int32]

指定したインデックス位置にある値を取得または設定します。 Gets or sets the value at the specified index.

Item[Object] Item[Object] Item[Object] Item[Object]

指定したキーの値を取得または設定します。 Gets or sets the value with the specified key.

Keys Keys Keys Keys

OrderedDictionary コレクションのキーを保持している ICollection オブジェクトを取得します。 Gets an ICollection object containing the keys in the OrderedDictionary collection.

Values Values Values Values

OrderedDictionary コレクションの値を保持している ICollection オブジェクトを取得します。 Gets an ICollection object containing the values in the OrderedDictionary collection.

方法

Add(Object, Object) Add(Object, Object) Add(Object, Object) Add(Object, Object)

指定したキーおよび値を持つエントリを、使用できる最小のインデックスを持つ OrderedDictionary コレクションに追加します。 Adds an entry with the specified key and value into the OrderedDictionary collection with the lowest available index.

AsReadOnly() AsReadOnly() AsReadOnly() AsReadOnly()

現在の OrderedDictionary コレクションの読み取り専用のコピーを返します。 Returns a read-only copy of the current OrderedDictionary collection.

Clear() Clear() Clear() Clear()

OrderedDictionary コレクションからすべての要素を削除します。 Removes all elements from the OrderedDictionary collection.

Contains(Object) Contains(Object) Contains(Object) Contains(Object)

OrderedDictionary コレクションに特定のキーが格納されているかどうかを判断します。 Determines whether the OrderedDictionary collection contains a specific key.

CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32)

1 次元の Array オブジェクトの指定したインデックスに OrderedDictionary の要素をコピーします。 Copies the OrderedDictionary elements to a one-dimensional Array object at the specified index.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

指定したオブジェクトが、現在のオブジェクトと等しいかどうかを判断します。 Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

OrderedDictionary コレクションを反復処理する IDictionaryEnumerator オブジェクトを返します。 Returns an IDictionaryEnumerator object that iterates through the OrderedDictionary collection.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

既定のハッシュ関数として機能します。 Serves as the default hash function.

(Inherited from Object)
GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext)

ISerializable インターフェイスを実装し、OrderedDictionary コレクションをシリアル化するために必要なデータを返します。 Implements the ISerializable interface and returns the data needed to serialize the OrderedDictionary collection.

GetType() GetType() GetType() GetType()

現在のインスタンスの Type を取得します。 Gets the Type of the current instance.

(Inherited from Object)
Insert(Int32, Object, Object) Insert(Int32, Object, Object) Insert(Int32, Object, Object) Insert(Int32, Object, Object)

OrderedDictionary コレクションの指定したインデックス位置に、指定したキーと値を持つ新しいエントリを挿入します。 Inserts a new entry into the OrderedDictionary collection with the specified key and value at the specified index.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

現在の Object の簡易コピーを作成します。 Creates a shallow copy of the current Object.

(Inherited from Object)
OnDeserialization(Object) OnDeserialization(Object) OnDeserialization(Object) OnDeserialization(Object)

ISerializable インターフェイスを実装し、逆シリアル化が完了したときに逆シリアル化イベントによってコールバックされます。 Implements the ISerializable interface and is called back by the deserialization event when deserialization is complete.

Remove(Object) Remove(Object) Remove(Object) Remove(Object)

指定したキーを持つエントリを OrderedDictionary コレクションから削除します。 Removes the entry with the specified key from the OrderedDictionary collection.

RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32) RemoveAt(Int32)

指定したインデックス位置にあるエントリを OrderedDictionary コレクションから削除します。 Removes the entry at the specified index from the OrderedDictionary collection.

ToString() ToString() ToString() ToString()

現在のオブジェクトを表す文字列を返します。 Returns a string that represents the current object.

(Inherited from Object)

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

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

OrderedDictionary オブジェクトへのアクセスが同期されている (スレッド セーフである) かどうかを示す値を取得します。 Gets a value indicating whether access to the OrderedDictionary object is synchronized (thread-safe).

ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

OrderedDictionary オブジェクトへのアクセスを同期するために使用できるオブジェクトを取得します。 Gets an object that can be used to synchronize access to the OrderedDictionary object.

IDeserializationCallback.OnDeserialization(Object) IDeserializationCallback.OnDeserialization(Object) IDeserializationCallback.OnDeserialization(Object) IDeserializationCallback.OnDeserialization(Object)

ISerializable インターフェイスを実装し、逆シリアル化が完了したときに逆シリアル化イベントによってコールバックされます。 Implements the ISerializable interface and is called back by the deserialization event when deserialization is complete.

IDictionary.IsFixedSize IDictionary.IsFixedSize IDictionary.IsFixedSize IDictionary.IsFixedSize

OrderedDictionary が固定サイズかどうかを示す値を取得します。 Gets a value indicating whether the OrderedDictionary has a fixed size.

IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator()

OrderedDictionary コレクションを反復処理する IDictionaryEnumerator オブジェクトを返します。 Returns an IDictionaryEnumerator object that iterates through the OrderedDictionary collection.

適用対象