IOrderedDictionary Interface

Represents an indexed collection of key/value pairs.

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

public interface IOrderedDictionary : IDictionary, 
	ICollection, IEnumerable

The IOrderedDictionary type exposes the following members.

  NameDescription
Public propertyCountGets the number of elements contained in the ICollection. (Inherited from ICollection.)
Public propertyIsFixedSizeGets a value indicating whether the IDictionary object has a fixed size. (Inherited from IDictionary.)
Public propertyIsReadOnlyGets a value indicating whether the IDictionary object is read-only. (Inherited from IDictionary.)
Public propertyIsSynchronizedGets a value indicating whether access to the ICollection is synchronized (thread safe). (Inherited from ICollection.)
Public propertyItem[Int32]Gets or sets the element at the specified index.
Public propertyItem[Object]Gets or sets the element with the specified key. (Inherited from IDictionary.)
Public propertyKeysGets an ICollection object containing the keys of the IDictionary object. (Inherited from IDictionary.)
Public propertySyncRootGets an object that can be used to synchronize access to the ICollection. (Inherited from ICollection.)
Public propertyValuesGets an ICollection object containing the values in the IDictionary object. (Inherited from IDictionary.)
Top

  NameDescription
Public methodAddAdds an element with the provided key and value to the IDictionary object. (Inherited from IDictionary.)
Public methodClearRemoves all elements from the IDictionary object. (Inherited from IDictionary.)
Public methodContainsDetermines whether the IDictionary object contains an element with the specified key. (Inherited from IDictionary.)
Public methodCopyToCopies the elements of the ICollection to an Array, starting at a particular Array index. (Inherited from ICollection.)
Public methodGetEnumeratorReturns an enumerator that iterates through the IOrderedDictionary collection.
Public methodInsertInserts a key/value pair into the collection at the specified index.
Public methodRemoveRemoves the element with the specified key from the IDictionary object. (Inherited from IDictionary.)
Public methodRemoveAtRemoves the element at the specified index.
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

IOrderedDictionary elements can be accessed either with the key or with the index.

Each element is a key/value pair stored in a DictionaryEntry structure.

Each pair must have a unique key that is not null, but the value can be null and does not have to be unique. The IOrderedDictionary interface allows the contained keys and values to be enumerated, but it does not imply any particular sort order.

The foreach statement of the C# language (For Each in Visual Basic) requires the type of each element in the collection. Because each element of the IDictionary 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, as the following example shows.

foreach (DictionaryEntry de in myOrderedDictionary)
{
    //...
}

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

Notes to Implementers

The implementing class must have a means to compare keys.

The following code example demonstrates the implementation of a simple IOrderedDictionary based on the ArrayList class. The implemented IOrderedDictionary stores first names as the keys and last names as the values, with the added requirement that each first name is unique.

using System;
using System.Collections;
using System.Collections.Specialized;

public class People : IOrderedDictionary
{
    private ArrayList _people;

    public People(int numItems)
    {
        _people = new ArrayList(numItems);
    }

    public int IndexOfKey(object key)
    {
        for (int i = 0; i < _people.Count; i++)
        {
            if (((DictionaryEntry)_people[i]).Key == key)
                return i;
        }

        // key not found, reutrn -1. 
        return -1;
    }

    public object this[object key]
    {
        get
        {
            return ((DictionaryEntry)_people[IndexOfKey(key)]).Value;
        }
        set
        {
            _people[IndexOfKey(key)] = new DictionaryEntry(key, value);
        }
    }

    // IOrderedDictionary Members 
    public IDictionaryEnumerator GetEnumerator()
    {
        return new PeopleEnum(_people);
    }

    public void Insert(int index, object key, object value)
    {
        if (IndexOfKey(key) != -1)
        {
            throw new ArgumentException("An element with the same key already exists in the collection.");
        }
        _people.Insert(index, new DictionaryEntry(key, value));
    }

    public void RemoveAt(int index)
    {
        _people.RemoveAt(index);
    }

    public object this[int index]
    {
        get
        {
            return ((DictionaryEntry)_people[index]).Value;
        }
        set
        {
            object key = ((DictionaryEntry)_people[index]).Key;
            _people[index] = new DictionaryEntry(Keys, value);
        }
    }
    // IDictionary Members 

    public void Add(object key, object value)
    {
        if (IndexOfKey(key) != -1)
        {
            throw new ArgumentException("An element with the same key already exists in the collection.");
        }
        _people.Add(new DictionaryEntry(key, value));
    }

    public void Clear()
    {
        _people.Clear();
    }

    public bool Contains(object key)
    {
        if (IndexOfKey(key) == -1)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    public bool IsFixedSize
    {
        get
        {
            return false;
        }
    }

    public bool IsReadOnly
    {
        get
        {
            return false;
        }
    }

    public ICollection Keys
    {
        get
        {
            ArrayList KeyCollection = new ArrayList(_people.Count);
            for (int i = 0; i < _people.Count; i++)
            {
                KeyCollection.Add( ((DictionaryEntry)_people[i]).Key );
            }
            return KeyCollection;
        }
    }

    public void Remove(object key)
    {
        _people.RemoveAt(IndexOfKey(key));
    }

    public ICollection Values
    {
        get
        {
            ArrayList ValueCollection = new ArrayList(_people.Count);
            for (int i = 0; i < _people.Count; i++)
            {
                ValueCollection.Add( ((DictionaryEntry)_people[i]).Value );
            }
            return ValueCollection;
        }
    }


    // ICollection Members 

    public void CopyTo(Array array, int index)
    {
        _people.CopyTo(array, index);
    }

    public int Count
    {
        get
        {
            return _people.Count;
        }
    }

    public bool IsSynchronized
    {
        get
        {
            return _people.IsSynchronized;
        }
    }

    public object SyncRoot
    {
        get
        {
            return _people.SyncRoot;
        }
    }

    // IEnumerable Members

    IEnumerator IEnumerable.GetEnumerator()
    {
        return new PeopleEnum(_people);
    }
}

public class PeopleEnum : IDictionaryEnumerator
{
    public ArrayList _people;

    // Enumerators are positioned before the first element 
    // until the first MoveNext() call. 
    int position = -1;

    public PeopleEnum(ArrayList list)
    {
        _people = list;
    }

    public bool MoveNext()
    {
        position++;
        return (position < _people.Count);
    }

    public void Reset()
    {
        position = -1;
    }

    public object Current
    {
        get
        {
            try
            {
                return _people[position];
            }
            catch (IndexOutOfRangeException)
            {
                throw new InvalidOperationException();
            }
        }
    }

    public DictionaryEntry Entry
    {
        get
        {
            return (DictionaryEntry)Current;
        }
    }

    public object Key
    {
        get
        {
            try
            {
                return ((DictionaryEntry)_people[position]).Key;
            }
            catch (IndexOutOfRangeException)
            {
                throw new InvalidOperationException();
            }
        }
    }

    public object Value
    {
        get
        {
            try
            {
                return ((DictionaryEntry)_people[position]).Value;
            }
            catch (IndexOutOfRangeException)
            {
                throw new InvalidOperationException();
            }
        }
    }
}

class App
{
    static void Main()
    {
        People peopleCollection = new People(3);
        peopleCollection.Add("John", "Smith");
        peopleCollection.Add("Jim", "Johnson");
        peopleCollection.Add("Sue", "Rabon");

        Console.WriteLine("Displaying the entries in peopleCollection:");
        foreach (DictionaryEntry de in peopleCollection)
        {
            Console.WriteLine("{0} {1}", de.Key, de.Value);
        }

        Console.WriteLine();
        Console.WriteLine("Displaying the entries in the modified peopleCollection:");
        peopleCollection["Jim"] = "Jackson";
        peopleCollection.Remove("Sue");
        peopleCollection.Insert(0, "Fred", "Anderson");

        foreach (DictionaryEntry de in peopleCollection)
        {
            Console.WriteLine("{0} {1}", de.Key, de.Value);
        }

    }
}
/* This code produces output similar to the following:
 * 
 * Displaying the entries in peopleCollection:
 * John Smith
 * Jim Johnson
 * Sue Rabon
 * 
 * Displaying the entries in the modified peopleCollection:
 * Fred Anderson
 * John Smith
 * Jim Jackson
 */

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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft