OrderedDictionary Class
Represents a collection of key/value pairs that are accessible by the key or index.
Assembly: System (in System.dll)
System.Collections.Specialized::OrderedDictionary
System.Web.Configuration::AdapterDictionary
| Name | Description | |
|---|---|---|
![]() | OrderedDictionary() | Initializes a new instance of the OrderedDictionary class. |
![]() | OrderedDictionary(IEqualityComparer^) | Initializes a new instance of the OrderedDictionary class using the specified comparer. |
![]() | OrderedDictionary(Int32) | Initializes a new instance of the OrderedDictionary class using the specified initial capacity. |
![]() | OrderedDictionary(Int32, IEqualityComparer^) | Initializes a new instance of the OrderedDictionary class using the specified initial capacity and comparer. |
![]() | OrderedDictionary(SerializationInfo^, StreamingContext) | Initializes a new instance of the OrderedDictionary class that is serializable using the specified SerializationInfo and StreamingContext objects. |
| Name | Description | |
|---|---|---|
![]() | Count | Gets the number of key/values pairs contained in the OrderedDictionary collection. |
![]() | IsReadOnly | Gets a value indicating whether the OrderedDictionary collection is read-only. |
![]() | Item[Int32] | Gets or sets the value at the specified index. |
![]() | Item[Object^] | Gets or sets the value with the specified key. |
![]() | Keys | Gets an ICollection object containing the keys in the OrderedDictionary collection. |
![]() | Values | Gets an ICollection object containing the values in the OrderedDictionary collection. |
| Name | Description | |
|---|---|---|
![]() | Add(Object^, Object^) | Adds an entry with the specified key and value into the OrderedDictionary collection with the lowest available index. |
![]() | AsReadOnly() | Returns a read-only copy of the current OrderedDictionary collection. |
![]() | Clear() | Removes all elements from the OrderedDictionary collection. |
![]() | Contains(Object^) | Determines whether the OrderedDictionary collection contains a specific key. |
![]() | CopyTo(Array^, Int32) | Copies the OrderedDictionary elements to a one-dimensional Array object at the specified index. |
![]() | Equals(Object^) | Determines whether the specified object is equal to the current object.(Inherited from Object.) |
![]() | Finalize() | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.) |
![]() | GetEnumerator() | Returns an IDictionaryEnumerator object that iterates through the OrderedDictionary collection. |
![]() | GetHashCode() | Serves as the default hash function. (Inherited from Object.) |
![]() | GetObjectData(SerializationInfo^, StreamingContext) | Implements the ISerializable interface and returns the data needed to serialize the OrderedDictionary collection. |
![]() | GetType() | |
![]() | Insert(Int32, Object^, Object^) | Inserts a new entry into the OrderedDictionary collection with the specified key and value at the specified index. |
![]() | MemberwiseClone() | |
![]() | OnDeserialization(Object^) | Implements the ISerializable interface and is called back by the deserialization event when deserialization is complete. |
![]() | Remove(Object^) | Removes the entry with the specified key from the OrderedDictionary collection. |
![]() | RemoveAt(Int32) | Removes the entry at the specified index from the OrderedDictionary collection. |
![]() | ToString() | Returns a string that represents the current object.(Inherited from Object.) |
| Name | Description | |
|---|---|---|
![]() ![]() | IEnumerable::GetEnumerator() | This API supports the product infrastructure and is not intended to be used directly from your code. Returns an IDictionaryEnumerator object that iterates through the OrderedDictionary collection. |
![]() ![]() | IDeserializationCallback::OnDeserialization(Object^) | Implements the ISerializable interface and is called back by the deserialization event when deserialization is complete. |
![]() ![]() | ICollection::IsSynchronized | Gets a value indicating whether access to the OrderedDictionary object is synchronized (thread-safe). |
![]() ![]() | ICollection::SyncRoot | Gets an object that can be used to synchronize access to the OrderedDictionary object. |
![]() ![]() | IDictionary::IsFixedSize | Gets a value indicating whether the OrderedDictionary has a fixed size. |
| Name | Description | |
|---|---|---|
![]() | AsParallel() | Overloaded. Enables parallelization of a query.(Defined by ParallelEnumerable.) |
![]() | AsQueryable() | Overloaded. Converts an IEnumerable to an IQueryable.(Defined by Queryable.) |
![]() | Cast<TResult>() | Casts the elements of an IEnumerable to the specified type.(Defined by Enumerable.) |
![]() | OfType<TResult>() | Filters the elements of an IEnumerable based on a specified type.(Defined by Enumerable.) |
Each element is a key/value pair stored in a DictionaryEntry object. A key cannot be null, 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) returns objects that are of 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.
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 */
Available since 10
.NET Framework
Available since 2.0
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.





