ListDictionary.Add() मेथड निर्दिष्ट कुंजी और मान को लिस्ट डिक्शनरी में जोड़ता है। ListDictionary छोटे संग्रह (आमतौर पर 10 या कम आइटम) के लिए सिंगली लिंक्ड लिस्ट आधारित कार्यान्वयन है।
उदाहरण 1: बुनियादी Add और एन्यूमरेटर इटरेशन
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary dict = new ListDictionary();
dict.Add("1", "One");
dict.Add("2", "Two");
dict.Add("3", "Three");
dict.Add("4", "Four");
dict.Add("5", "Five");
Console.WriteLine("ListDictionary की-वैल्यू पेयर्स...");
IDictionaryEnumerator enumerator = dict.GetEnumerator();
while (enumerator.MoveNext()) {
Console.WriteLine("Key = " + enumerator.Key + ", Value = " + enumerator.Value);
}
}
}
आउटपुट
ListDictionary की-वैल्यू पेयर्स...
Key = 1, Value = One
Key = 2, Value = Two
Key = 3, Value = Three
Key = 4, Value = Four
Key = 5, Value = Five
उदाहरण 2: इंटीजर वैल्यूज़ और Keys संग्रह
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary dict = new ListDictionary();
dict.Add("1", 100);
dict.Add("2", 200);
dict.Add("3", 300);
dict.Add("4", 400);
dict.Add("5", 500);
dict.Add("6", 600);
dict.Add("7", 700);
dict.Add("8", 800);
dict.Add("9", 900);
dict.Add("10", 1000);
// सभी कुंजियाँ प्रदर्शित करें
ICollection keys = dict.Keys;
Console.WriteLine("सभी कुंजियाँ प्रदर्शित...");
foreach (string key in keys) {
Console.WriteLine(key);
}
}
}
आउटपुट
सभी कुंजियाँ प्रदर्शित...
1
2
3
4
5
6
7
8
9
10
ListDictionary मुख्य विशेषताएं
- छोटे डेटा सेट के लिए अनुकूलित: ~10 आइटम्स तक Hashtable से तेज़।
- लिंक्ड लिस्ट कार्यान्वयन: सिंगली लिंक्ड लिस्ट (O(n) लुकअप)।
- इंसर्शन क्रम: तत्व इंसर्शन क्रम में बने रहते हैं।
- केस-सेंसिटिव कुंजियाँ: डिफ़ॉल्ट रूप से केस-सेंसिटिव तुलना।
- नॉन-जेनेरिक: कुंजी और मान
objectहैं।
ListDictionary बनाम HybridDictionary बनाम Hashtable
| क्लास | सर्वोत्तम उपयोग | कार्यान्वयन |
|---|---|---|
| ListDictionary | बहुत छोटे संग्रह (≤10) | सिंगली लिंक्ड लिस्ट |
| HybridDictionary | चर आकार (स्वचालित स्विच) | ListDictionary → Hashtable |
| Hashtable | बड़े संग्रह | हैश टेबल |
| Dictionary<TKey,TValue> | सभी नए कोड | जेनेरिक हैश टेबल |
सामान्य ऑपरेशन
ListDictionary dict = new ListDictionary();
dict.Add("key", "value"); // जोड़ें
string val = (string)dict["key"]; // इंडेक्सर से प्राप्त करें
bool exists = dict.Contains("key"); // कुंजी जांचें
dict.Remove("key"); // हटाएं
dict.Clear(); // सभी हटाएं
int count = dict.Count; // संख्या
नोट
ListDictionary लीगेसी क्लास है (System.Collections.Specialized)। नए कोड के लिए Dictionary<TKey, TValue> उपयोग करें जो प्रकार-सुरक्षित, तेज़ और अधिक सुविधाजनक है।