C# में ListDictionary वर्ग की सभी कुंजियों को ICollection के रूप में प्राप्त करने के लिए Keys प्रॉपर्टी का उपयोग किया जाता है। यह प्रॉपर्टी डिक्शनरी में मौजूद सभी कुंजियों का संग्रह लौटाती है, जिसे आप foreach लूप से पुनरावृत्त कर सकते हैं।
उदाहरण 1: संख्यात्मक कुंजियाँ
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary listDict = new ListDictionary();
listDict.Add("1", "Laptop");
listDict.Add("2", "Tablet");
listDict.Add("3", "Desktop");
listDict.Add("4", "Speaker");
listDict.Add("5", "Earphone");
listDict.Add("6", "Headphone");
ICollection col = listDict.Keys;
foreach(String s in col) {
Console.WriteLine(s);
}
}
}
आउटपुट
1 2 3 4 5 6
उदाहरण 2: अक्षर कुंजियाँ
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary listDict = new ListDictionary();
listDict.Add("A", "Tom");
listDict.Add("B", "John");
listDict.Add("C", "Kevin");
listDict.Add("D", "Tim");
ICollection col = listDict.Keys;
foreach(String s in col) {
Console.WriteLine(s);
}
}
}
आउटपुट
A B C D
मुख्य बिंदु
ListDictionary.Keysप्रॉपर्टीICollectionलौटाती हैforeachलूप से कुंजियों को आसानी से पुनरावृत्त कर सकते हैंListDictionaryछोटे डेटा सेट के लिए अनुकूलित है