OrderedDictionary.Keys गुण एक ICollection लौटाता है जिसमें ऑर्डर्ड डिक्शनरी की कुंजियाँ सम्मिलन क्रम में होती हैं। OrderedDictionary कुंजियों के सम्मिलन क्रम को बनाए रखता है।
उदाहरण 1: संख्यात्मक कुंजियाँ
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo
{
public static void Main()
{
OrderedDictionary dict = new OrderedDictionary();
dict.Add("1", "One");
dict.Add("2", "Two");
dict.Add("3", "Three");
dict.Add("4", "Four");
dict.Add("5", "Five");
dict.Add("6", "Six");
dict.Add("7", "Seven");
dict.Add("8", "Eight");
ICollection col = dict.Keys;
String[] strKeys = new String[dict.Count];
col.CopyTo(strKeys, 0);
Console.WriteLine("Displaying the keys...");
for (int i = 0; i < dict.Count; i++)
{
Console.WriteLine(strKeys[i]);
}
}
}
आउटपुट
Displaying the keys... 1 2 3 4 5 6 7 8
उदाहरण 2: स्ट्रिंग कुंजियाँ
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo
{
public static void Main()
{
OrderedDictionary dict = new OrderedDictionary();
dict.Add("One", "John");
dict.Add("Two", "Tim");
dict.Add("Three", "Katie");
dict.Add("Four", "Andy");
dict.Add("Five", "Gary");
dict.Add("Six", "Amy");
ICollection col = dict.Keys;
String[] strKeys = new String[dict.Count];
col.CopyTo(strKeys, 0);
Console.WriteLine("Displaying the keys...");
for (int i = 0; i < dict.Count; i++)
{
Console.WriteLine(strKeys[i]);
}
}
}
आउटपुट
Displaying the keys... One Two Three Four Five Six
OrderedDictionary (नामस्थान System.Collections.Specialized) सम्मिलन क्रम बनाए रखता है और कुंजी/इंडेक्स दोनों द्वारा पहुंच की अनुमति देता है। Keys और Values संग्रह लाइव दृश्य हैं। आधुनिक विकल्प: Dictionary<TKey, TValue> (क्रम नहीं) या तृतीय-पक्ष ऑर्डर्ड डिक्शनरी।