Export (0) Print
Expand All

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)

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

The OrderedDictionary type exposes the following members.

  NameDescription
Public methodOrderedDictionaryInitializes 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 the default hash function. (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(Of TResult)Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodOfType(Of 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 Nothing, but a value can be.

The elements of an OrderedDictionary are not sorted by the key, unlike the elements of a SortedDictionary(Of 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 de As DictionaryEntry In myOrderedDictionary
    '... 
Next de

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)

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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.
Show:
© 2014 Microsoft