परिचय
ListDictionary क्लास IDictionary को एक सिंगली लिंक्ड लिस्ट का उपयोग करके लागू करती है। यह छोटे संग्रहों (आमतौर पर 10 से कम आइटम) के लिए अनुशंसित है।
गुण
| क्र.सं. | गुण और विवरण |
| 1 | Count - की/मान जोड़े की संख्या लौटाता है। |
| 2 | IsFixedSize - निश्चित आकार है या नहीं दर्शाता है। |
| 3 | IsReadOnly - रीड-ओनली है या नहीं दर्शाता है। |
| 4 | IsSynchronized - थ्रेड-सेफ है या नहीं दर्शाता है। |
| 5 | Item[Object] - निर्दिष्ट कुंजी से जुड़ा मान प्राप्त/सेट करता है। |
| 6 | Keys - कुंजियों का संग्रह लौटाता है। |
| 7 | SyncRoot - सिंक्रनाइज़ेशन के लिए ऑब्जेक्ट लौटाता है। |
| 8 | Values - मानों का संग्रह लौटाता है। |
मुख्य मेथड्स
| क्र.सं. | मेथड और विवरण |
| 1 | Add(Object, Object) - निर्दिष्ट कुंजी और मान के साथ एंट्री जोड़ता है। |
| 2 | Clear() - सभी एंट्रीज़ हटाता है। |
| 3 | Contains(Object) - विशिष्ट कुंजी मौजूद है या नहीं जांचता है। |
| 4 | CopyTo(Array, Int32) - एंट्रीज़ को एक-आयामी सरणी में कॉपी करता है। |
| 5 | GetEnumerator() - IDictionaryEnumerator लौटाता है। |
उदाहरण 1: गुणों की जांच
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary dict1 = new ListDictionary();
dict1.Add("A", "Books");
dict1.Add("B", "Electronics");
dict1.Add("C", "Smart Wearables");
dict1.Add("D", "Pet Supplies");
dict1.Add("E", "Clothing");
dict1.Add("F", "Footwear");
Console.WriteLine("ListDictionary1 तत्व...");
foreach(DictionaryEntry d in dict1) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("क्या ListDictionary1 का आकार निश्चित है? = " + dict1.IsFixedSize);
Console.WriteLine("क्या ListDictionary1 रीड-ओनली है? = " + dict1.IsReadOnly);
Console.WriteLine("क्या ListDictionary1 सिंक्रनाइज़्ड है? = " + dict1.IsSynchronized);
Console.WriteLine("क्या ListDictionary1 में कुंजी M है? = " + dict1.Contains("M"));
ListDictionary dict2 = new ListDictionary();
dict2.Add("1", "One");
dict2.Add("2", "Two");
dict2.Add("3", "Three");
dict2.Add("4", "Four");
dict2.Add("5", "Five");
dict2.Add("6", "Six");
Console.WriteLine("\nListDictionary2 कुंजी-मान जोड़े...");
IDictionaryEnumerator demoEnum = dict2.GetEnumerator();
while (demoEnum.MoveNext())
Console.WriteLine("कुंजी = " + demoEnum.Key + ", मान = " + demoEnum.Value);
Console.WriteLine("क्या ListDictionary2 का आकार निश्चित है? = " + dict2.IsFixedSize);
Console.WriteLine("क्या ListDictionary2 रीड-ओनली है? = " + dict2.IsReadOnly);
Console.WriteLine("क्या ListDictionary2 सिंक्रनाइज़्ड है? = " + dict2.IsSynchronized);
Console.WriteLine("क्या ListDictionary2 में कुंजी 5 है? = " + dict2.Contains("5"));
}
}
आउटपुट
ListDictionary1 तत्व...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
क्या ListDictionary1 का आकार निश्चित है? = False
क्या ListDictionary1 रीड-ओनली है? = False
क्या ListDictionary1 सिंक्रनाइज़्ड है? = False
क्या ListDictionary1 में कुंजी M है? = False
ListDictionary2 कुंजी-मान जोड़े...
कुंजी = 1, मान = One
कुंजी = 2, मान = Two
कुंजी = 3, मान = Three
कुंजी = 4, मान = Four
कुंजी = 5, मान = Five
कुंजी = 6, मान = Six
क्या ListDictionary2 का आकार निश्चित है? = False
क्या ListDictionary2 रीड-ओनली है? = False
क्या ListDictionary2 सिंक्रनाइज़्ड है? = False
क्या ListDictionary2 में कुंजी 5 है? = True
उदाहरण 2: दो ListDictionary ऑब्जेक्ट्स की तुलना
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary dict1 = new ListDictionary();
dict1.Add("A", "Books");
dict1.Add("B", "Electronics");
dict1.Add("C", "Smart Wearables");
dict1.Add("D", "Pet Supplies");
dict1.Add("E", "Clothing");
dict1.Add("F", "Footwear");
ListDictionary dict2 = new ListDictionary();
dict2.Add("1", "One");
dict2.Add("2", "Two");
dict2.Add("3", "Three");
dict2.Add("4", "Four");
dict2.Add("5", "Five");
dict2.Add("6", "Six");
ListDictionary dict3 = new ListDictionary();
dict3 = dict2;
Console.WriteLine("क्या ListDictionary3, ListDictionary2 के बराबर है? = " + (dict3.Equals(dict2)));
}
}
आउटपुट
क्या ListDictionary3, ListDictionary2 के बराबर है? = True
मुख्य बिंदु
- छोटे डेटा सेट के लिए
ListDictionary अधिक कुशल है।
- यह थ्रेड-सेफ नहीं है; सिंक्रनाइज़ेशन के लिए
SyncRoot का उपयोग करें।
Equals रेफरेंस समानता की जांच करता है, सामग्री समानता की नहीं।