OrderedDictionary Klasa

Definicja

Reprezentuje kolekcję par klucz/wartość, które są dostępne dla klucza lub indeksu.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
public ref class OrderedDictionary : System::Collections::IDictionary, System::Collections::Specialized::IOrderedDictionary, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
public ref class OrderedDictionary : System::Collections::Specialized::IOrderedDictionary, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
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
type OrderedDictionary = class
    interface ICollection
    interface IEnumerable
    interface IDictionary
    interface IOrderedDictionary
type OrderedDictionary = class
    interface ICollection
    interface IEnumerable
    interface IDictionary
    interface IOrderedDictionary
    interface IDeserializationCallback
    interface ISerializable
type OrderedDictionary = class
    interface ICollection
    interface IEnumerable
    interface IDictionary
    interface IOrderedDictionary
    interface ISerializable
    interface IDeserializationCallback
[<System.Serializable>]
type OrderedDictionary = class
    interface IOrderedDictionary
    interface IDictionary
    interface ICollection
    interface IEnumerable
    interface ISerializable
    interface IDeserializationCallback
[<System.Serializable>]
type OrderedDictionary = class
    interface IOrderedDictionary
    interface ISerializable
    interface IDeserializationCallback
    interface IDictionary
    interface ICollection
    interface IEnumerable
Public Class OrderedDictionary
Implements IDictionary, IOrderedDictionary
Public Class OrderedDictionary
Implements IDeserializationCallback, IDictionary, IOrderedDictionary, ISerializable
Public Class OrderedDictionary
Implements IDeserializationCallback, IOrderedDictionary, ISerializable
Dziedziczenie
OrderedDictionary
Pochodne
Atrybuty
Implementuje

Przykłady

Poniższy przykład kodu demonstruje tworzenie, populację i modyfikację OrderedDictionary kolekcji, a także dwie techniki wyświetlania zawartości OrderedDictionary : jeden przy użyciu Keys Values właściwości i i innych tworzenia modułu wyliczającego za pomocą GetEnumerator metody.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);
    }

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

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

Uwagi

Każdy element jest parą klucz/wartość przechowywaną w DictionaryEntry obiekcie.Each element is a key/value pair stored in a DictionaryEntry object. Klucz nie może być null , ale może być wartością.A key cannot be null, but a value can be.

Elementy elementu OrderedDictionary nie są posortowane według klucza, w przeciwieństwie do elementów SortedDictionary<TKey,TValue> klasy.The elements of an OrderedDictionary are not sorted by the key, unlike the elements of a SortedDictionary<TKey,TValue> class. Możesz uzyskać dostęp do elementów za pomocą klucza lub indeksu.You can access elements either by the key or by the index.

foreachInstrukcja języka C# ( For Each w Visual Basic) zwraca obiekty, które są typu każdego elementu w kolekcji.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. Ponieważ każdy element OrderedDictionary kolekcji jest parą klucz/wartość, typ elementu nie jest typem klucza lub typem wartości.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. Zamiast tego typ elementu to DictionaryEntry .Instead, the element type is DictionaryEntry. Poniższy kod przedstawia składnię języka C#, Visual Basic i 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

foreachInstrukcja to otoka wokół modułu wyliczającego, która umożliwia odczyt z kolekcji.The foreach statement is a wrapper around the enumerator, which only allows reading from, not writing to, the collection.

Konstruktory

OrderedDictionary()

Inicjuje nowe wystąpienie klasy OrderedDictionary.Initializes a new instance of the OrderedDictionary class.

OrderedDictionary(IEqualityComparer)

Inicjuje nowe wystąpienie OrderedDictionary klasy przy użyciu określonej metody porównującej.Initializes a new instance of the OrderedDictionary class using the specified comparer.

OrderedDictionary(Int32)

Inicjuje nowe wystąpienie OrderedDictionary klasy przy użyciu określonej początkowej pojemności.Initializes a new instance of the OrderedDictionary class using the specified initial capacity.

OrderedDictionary(Int32, IEqualityComparer)

Inicjuje nowe wystąpienie OrderedDictionary klasy przy użyciu określonej początkowej pojemności i funkcji porównującej.Initializes a new instance of the OrderedDictionary class using the specified initial capacity and comparer.

OrderedDictionary(SerializationInfo, StreamingContext)

Inicjuje nowe wystąpienie OrderedDictionary klasy, która jest możliwa do serializacji przy użyciu określonych SerializationInfo StreamingContext obiektów i.Initializes a new instance of the OrderedDictionary class that is serializable using the specified SerializationInfo and StreamingContext objects.

Właściwości

Count

Pobiera liczbę par klucz/wartość zawartych w OrderedDictionary kolekcji.Gets the number of key/values pairs contained in the OrderedDictionary collection.

IsReadOnly

Pobiera wartość wskazującą, czy OrderedDictionary kolekcja jest tylko do odczytu.Gets a value indicating whether the OrderedDictionary collection is read-only.

Item[Int32]

Pobiera lub ustawia wartość o określonym indeksie.Gets or sets the value at the specified index.

Item[Object]

Pobiera lub ustawia wartość z określonym kluczem.Gets or sets the value with the specified key.

Keys

Pobiera ICollection obiekt zawierający klucze w OrderedDictionary kolekcji.Gets an ICollection object containing the keys in the OrderedDictionary collection.

Values

Pobiera ICollection obiekt zawierający wartości w OrderedDictionary kolekcji.Gets an ICollection object containing the values in the OrderedDictionary collection.

Metody

Add(Object, Object)

Dodaje wpis z określonym kluczem i wartością do OrderedDictionary kolekcji z najniższym dostępnym indeksem.Adds an entry with the specified key and value into the OrderedDictionary collection with the lowest available index.

AsReadOnly()

Zwraca kopię z bieżącą kolekcją tylko do odczytu OrderedDictionary .Returns a read-only copy of the current OrderedDictionary collection.

Clear()

Usuwa wszystkie elementy z OrderedDictionary kolekcji.Removes all elements from the OrderedDictionary collection.

Contains(Object)

Określa, czy OrderedDictionary Kolekcja zawiera określony klucz.Determines whether the OrderedDictionary collection contains a specific key.

CopyTo(Array, Int32)

Kopiuje OrderedDictionary elementy do jednowymiarowego Array obiektu o określonym indeksie.Copies the OrderedDictionary elements to a one-dimensional Array object at the specified index.

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.Determines whether the specified object is equal to the current object.

(Odziedziczone po Object)
GetEnumerator()

Zwraca IDictionaryEnumerator obiekt, który wykonuje iterację w OrderedDictionary kolekcji.Returns an IDictionaryEnumerator object that iterates through the OrderedDictionary collection.

GetHashCode()

Służy jako domyślna funkcja skrótu.Serves as the default hash function.

(Odziedziczone po Object)
GetObjectData(SerializationInfo, StreamingContext)

Implementuje ISerializable interfejs i zwraca dane, które są konieczne do serializacji OrderedDictionary kolekcji.Implements the ISerializable interface and returns the data needed to serialize the OrderedDictionary collection.

GetType()

Pobiera Type bieżące wystąpienie.Gets the Type of the current instance.

(Odziedziczone po Object)
Insert(Int32, Object, Object)

Wstawia nowy wpis do OrderedDictionary kolekcji z określonym kluczem i wartością o określonym indeksie.Inserts a new entry into the OrderedDictionary collection with the specified key and value at the specified index.

MemberwiseClone()

Tworzy skróconą kopię bieżącego elementu Object .Creates a shallow copy of the current Object.

(Odziedziczone po Object)
OnDeserialization(Object)

Implementuje ISerializable interfejs i jest wywoływany ponownie przez zdarzenie deserializacji po zakończeniu deserializacji.Implements the ISerializable interface and is called back by the deserialization event when deserialization is complete.

Remove(Object)

Usuwa wpis z określonym kluczem z OrderedDictionary kolekcji.Removes the entry with the specified key from the OrderedDictionary collection.

RemoveAt(Int32)

Usuwa wpis z kolekcji w określonym indeksie OrderedDictionary .Removes the entry at the specified index from the OrderedDictionary collection.

ToString()

Zwraca ciąg reprezentujący bieżący obiekt.Returns a string that represents the current object.

(Odziedziczone po Object)

Jawne implementacje interfejsu

ICollection.IsSynchronized

Pobiera wartość wskazującą, czy dostęp do OrderedDictionary obiektu jest synchronizowany (bezpieczny wątkowo).Gets a value indicating whether access to the OrderedDictionary object is synchronized (thread-safe).

ICollection.SyncRoot

Pobiera obiekt, który może służyć do synchronizowania dostępu do OrderedDictionary obiektu.Gets an object that can be used to synchronize access to the OrderedDictionary object.

IDeserializationCallback.OnDeserialization(Object)

Implementuje ISerializable interfejs i jest wywoływany ponownie przez zdarzenie deserializacji po zakończeniu deserializacji.Implements the ISerializable interface and is called back by the deserialization event when deserialization is complete.

IDictionary.IsFixedSize

Pobiera wartość wskazującą, czy OrderedDictionary ma stały rozmiar.Gets a value indicating whether the OrderedDictionary has a fixed size.

IEnumerable.GetEnumerator()

Zwraca IDictionaryEnumerator obiekt, który wykonuje iterację w OrderedDictionary kolekcji.Returns an IDictionaryEnumerator object that iterates through the OrderedDictionary collection.

Metody rozszerzania

Cast<TResult>(IEnumerable)

Rzutuje elementy elementu IEnumerable do określonego typu.Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

Filtruje elementy IEnumerable w oparciu o określony typ.Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

Włącza przetwarzanie równoległe zapytania.Enables parallelization of a query.

AsQueryable(IEnumerable)

Konwertuje IEnumerable do IQueryable .Converts an IEnumerable to an IQueryable.

Dotyczy