Export (0) Print
Expand All

OrderedDictionary.Keys Property

Gets an ICollection object containing the keys in the OrderedDictionary collection.

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

public ICollection Keys { get; }
/** @property */
public final ICollection get_Keys ()

public final function get Keys () : ICollection

Not applicable.

Property Value

An ICollection object containing the keys in the OrderedDictionary collection.

The returned ICollection object is not a static copy; instead, the ICollection refers back to the keys in the original OrderedDictionary collection. Therefore, changes to the OrderedDictionary continue to be reflected in the ICollection.

The following code example demonstrates the creation and population of an OrderedDictionary collection, and then prints the contents to the console. In this example, the Keys and Values properties are passed to a method that displays the contents. This code is part of a larger code example that can be viewed at OrderedDictionary.

// Creates and initializes a OrderedDictionary.
OrderedDictionary myOrderedDictionary = new 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);

// Displays the contents of the OrderedDictionary from its keys and values
public static void DisplayContents(
    ICollection keyCollection, ICollection valueCollection, int dictionarySize)
{
    String[] myKeys = new String[dictionarySize];
    String[] myValues = new 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();
}

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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft