OrderedDictionary Třída

Definice

Představuje kolekci párů klíč/hodnota, které jsou přístupné klíčem nebo indexem.

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
Public Class OrderedDictionary
Implements IDictionary, IOrderedDictionary
Public Class OrderedDictionary
Implements IDeserializationCallback, IDictionary, IOrderedDictionary, ISerializable
Public Class OrderedDictionary
Implements IDeserializationCallback, IOrderedDictionary, ISerializable
Dědičnost
OrderedDictionary
Odvozené
Atributy
Implementuje

Příklady

Následující příklad kódu ukazuje vytvoření, populaci a úpravy OrderedDictionary kolekce, stejně jako dvě techniky k zobrazení obsahu OrderedDictionary: jeden pomocí Keys a Values vlastnosti a druhý vytvoření enumerátoru GetEnumerator prostřednictvím metody.

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

Poznámky

Každý prvek je pár klíč/hodnota uložený v objektu DictionaryEntry . Klíč nemůže být null, ale hodnota může být.

Prvky klíče OrderedDictionary nejsou seřazeny podle klíče, na rozdíl od prvků SortedDictionary<TKey,TValue> třídy. K prvkům můžete přistupovat buď klíčem, nebo indexem.

Příkaz foreach jazyka C# (For Each v Visual Basic) vrátí objekty, které jsou typu každého prvku v kolekci. Vzhledem k tomu, že každý prvek OrderedDictionary kolekce je pár klíč/hodnota, typ prvku není typem klíče nebo typu hodnoty. Místo toho je DictionaryEntrytyp prvku . Následující kód ukazuje syntaxi.

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

Příkaz foreach je obálka kolem enumerátoru, který umožňuje jen čtení z kolekce, nikoli zápis do.

Konstruktory

Name Description
OrderedDictionary()

Inicializuje novou instanci OrderedDictionary třídy.

OrderedDictionary(IEqualityComparer)

Inicializuje novou instanci OrderedDictionary třídy pomocí zadaného porovnávače.

OrderedDictionary(Int32, IEqualityComparer)

Inicializuje novou instanci OrderedDictionary třídy pomocí zadané počáteční kapacity a porovnávače.

OrderedDictionary(Int32)

Inicializuje novou instanci OrderedDictionary třídy pomocí zadané počáteční kapacity.

OrderedDictionary(SerializationInfo, StreamingContext)
Zastaralé.

Inicializuje novou instanci OrderedDictionary třídy, která je serializovatelná pomocí zadané SerializationInfo a StreamingContext objekty.

Vlastnosti

Name Description
Count

Získá počet párů klíč/hodnot obsažených v kolekci OrderedDictionary .

IsReadOnly

Získá hodnotu určující, zda OrderedDictionary kolekce je jen pro čtení.

Item[Int32]

Získá nebo nastaví hodnotu v zadaném indexu.

Item[Object]

Získá nebo nastaví hodnotu se zadaným klíčem.

Keys

ICollection Získá objekt obsahující klíče v kolekciOrderedDictionary.

Values

ICollection Získá objekt obsahující hodnoty v kolekciOrderedDictionary.

Metody

Name Description
Add(Object, Object)

Přidá položku se zadaným klíčem a hodnotou do OrderedDictionary kolekce s nejnižším dostupným indexem.

AsReadOnly()

Vrátí kopii aktuální OrderedDictionary kolekce jen pro čtení.

Clear()

Odebere všechny prvky z OrderedDictionary kolekce.

Contains(Object)

Určuje, zda OrderedDictionary kolekce obsahuje konkrétní klíč.

CopyTo(Array, Int32)

OrderedDictionary Zkopíruje prvky do jednorozměrného Array objektu v zadaném indexu.

Equals(Object)

Určuje, zda je zadaný objekt roven aktuálnímu objektu.

(Zděděno od Object)
GetEnumerator()

IDictionaryEnumerator Vrátí objekt, který prochází kolekcíOrderedDictionary.

GetHashCode()

Slouží jako výchozí funkce hash.

(Zděděno od Object)
GetObjectData(SerializationInfo, StreamingContext)
Zastaralé.

Implementuje ISerializable rozhraní a vrací data potřebná k serializaci OrderedDictionary kolekce.

GetType()

Získá Type aktuální instance.

(Zděděno od Object)
Insert(Int32, Object, Object)

Vloží novou položku do OrderedDictionary kolekce se zadaným klíčem a hodnotou v zadaném indexu.

MemberwiseClone()

Vytvoří mělkou kopii aktuálního Object.

(Zděděno od Object)
OnDeserialization(Object)

Implementuje ISerializable rozhraní a je volána zpět deserializační událostí při dokončení deserializace.

Remove(Object)

Odebere položku se zadaným klíčem z OrderedDictionary kolekce.

RemoveAt(Int32)

Odebere položku v zadaném indexu OrderedDictionary z kolekce.

ToString()

Vrátí řetězec, který představuje aktuální objekt.

(Zděděno od Object)

Explicitní implementace rozhraní

Name Description
ICollection.IsSynchronized

Získá hodnotu určující, zda je přístup k objektu OrderedDictionary synchronizován (thread-safe).

ICollection.SyncRoot

Získá objekt, který lze použít k synchronizaci přístupu k objektu OrderedDictionary .

IDeserializationCallback.OnDeserialization(Object)

Implementuje ISerializable rozhraní a je volána zpět deserializační událostí při dokončení deserializace.

IDictionary.IsFixedSize

Získá hodnotu určující, zda OrderedDictionary má pevnou velikost.

IEnumerable.GetEnumerator()

IDictionaryEnumerator Vrátí objekt, který prochází kolekcíOrderedDictionary.

Metody rozšíření

Name Description
AsParallel(IEnumerable)

Umožňuje paralelizaci dotazu.

AsQueryable(IEnumerable)

Převede IEnumerable na IQueryable.

Cast<TResult>(IEnumerable)

Přetypuje prvky IEnumerable na zadaný typ.

OfType<TResult>(IEnumerable)

Filtruje prvky IEnumerable na základě zadaného typu.

Platí pro