Get an ICollection containing the keys in OrderedDictionary in CSharp
Description
The following code shows how to get an ICollection containing the keys in OrderedDictionary.
Example
using System;// www . ja v a2 s.c om
using System.Collections;
using System.Collections.Specialized;
public class SamplesStringCollection {
public static void Main() {
OrderedDictionary myOrderedDictionary = new OrderedDictionary();
myOrderedDictionary.Add("testKey1", "testValue1");
myOrderedDictionary.Add("testKey2", "testValue2");
myOrderedDictionary.Add("testKey3", "testValue3");
ICollection keyCollection = myOrderedDictionary.Keys;
ICollection valueCollection = myOrderedDictionary.Values;
DisplayContents(keyCollection, valueCollection, myOrderedDictionary.Count);
}
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);
for (int i = 0; i < dictionarySize; i++)
{
Console.WriteLine(myKeys[i] + "" + myValues[i]);
}
}
}
The code above generates the following result.