Export (0) Print
Expand All
1 out of 4 rated this helpful - Rate this topic

OrderedDictionary Class

Represents a collection of key/value pairs that are accessible by the key or index.

System::Object
  System.Collections.Specialized::OrderedDictionary
    System.Web.Configuration::AdapterDictionary

Namespace:  System.Collections.Specialized
Assembly:  System (in System.dll)
[SerializableAttribute]
public ref class OrderedDictionary : IOrderedDictionary, 
	IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback

The OrderedDictionary type exposes the following members.

  NameDescription
Public methodOrderedDictionary()Initializes a new instance of the OrderedDictionary class.
Public methodOrderedDictionary(IEqualityComparer)Initializes a new instance of the OrderedDictionary class using the specified comparer.
Public methodOrderedDictionary(Int32)Initializes a new instance of the OrderedDictionary class using the specified initial capacity.
Public methodOrderedDictionary(Int32, IEqualityComparer)Initializes a new instance of the OrderedDictionary class using the specified initial capacity and comparer.
Protected methodOrderedDictionary(SerializationInfo, StreamingContext)Initializes a new instance of the OrderedDictionary class that is serializable using the specified SerializationInfo and StreamingContext objects.
Top
  NameDescription
Public propertyCountGets the number of key/values pairs contained in the OrderedDictionary collection.
Public propertyIsReadOnlyGets a value indicating whether the OrderedDictionary collection is read-only.
Public propertyItem[Int32]Gets or sets the value at the specified index.
Public propertyItem[Object]Gets or sets the value with the specified key.
Public propertyKeysGets an ICollection object containing the keys in the OrderedDictionary collection.
Public propertyValuesGets an ICollection object containing the values in the OrderedDictionary collection.
Top
  NameDescription
Public methodAddAdds an entry with the specified key and value into the OrderedDictionary collection with the lowest available index.
Public methodAsReadOnlyReturns a read-only copy of the current OrderedDictionary collection.
Public methodClearRemoves all elements from the OrderedDictionary collection.
Public methodContainsDetermines whether the OrderedDictionary collection contains a specific key.
Public methodCopyToCopies the OrderedDictionary elements to a one-dimensional Array object at the specified index.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetEnumeratorReturns an IDictionaryEnumerator object that iterates through the OrderedDictionary collection.
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetObjectDataImplements the ISerializable interface and returns the data needed to serialize the OrderedDictionary collection.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInsertInserts a new entry into the OrderedDictionary collection with the specified key and value at the specified index.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodOnDeserializationImplements the ISerializable interface and is called back by the deserialization event when deserialization is complete.
Public methodRemoveRemoves the entry with the specified key from the OrderedDictionary collection.
Public methodRemoveAtRemoves the entry at the specified index from the OrderedDictionary collection.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top
  NameDescription
Public Extension MethodAsParallelEnables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension MethodAsQueryableConverts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodCast<TResult>Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodOfType<TResult>Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top
  NameDescription
Explicit interface implemetationPrivate propertyICollection::IsSynchronizedGets a value indicating whether access to the OrderedDictionary object is synchronized (thread-safe).
Explicit interface implemetationPrivate propertyICollection::SyncRootGets an object that can be used to synchronize access to the OrderedDictionary object.
Explicit interface implemetationPrivate methodIDeserializationCallback::OnDeserializationImplements the ISerializable interface and is called back by the deserialization event when deserialization is complete.
Explicit interface implemetationPrivate propertyIDictionary::IsFixedSizeGets a value indicating whether the OrderedDictionary has a fixed size.
Explicit interface implemetationPrivate methodIEnumerable::GetEnumeratorInfrastructure. Returns an IDictionaryEnumerator object that iterates through the OrderedDictionary collection.
Top

Each element is a key/value pair stored in a DictionaryEntry object. A key cannot be nullptr, but a value can be.

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.

The foreach statement of the C# language (For Each in Visual Basic) requires the type of each element in the collection. 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. Instead, the element type is DictionaryEntry. The following code shows C#, Visual Basic and C++ syntax.


for each (DictionaryEntry de in myOrderedDictionary)
{
    //...
}


The foreach statement is a wrapper around the enumerator, which only allows reading from, not writing to, the collection.

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

*/


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.