This documentation is archived and is not being maintained.

OrderedDictionary Class

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

Namespace:  System.Collections.Specialized
Assembly:  System (in System.dll)

'Declaration
<SerializableAttribute> _
Public Class OrderedDictionary _
	Implements IOrderedDictionary, IDictionary, ICollection, IEnumerable,  _
	ISerializable, IDeserializationCallback
'Usage
Dim instance As OrderedDictionary

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

The elements of an OrderedDictionary are not sorted in any way. OrderedDictionary collections allow access by both index as well as key.

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# and Visual Basic syntax.

For Each de As DictionaryEntry In myListDictionary
  ...
Next de

[Visual Basic, C#]

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

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

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Show: