OrderedDictionary क्लास (System.Collections.Specialized) की-वैल्यू पेयर्स का संग्रह है जो इंसर्शन क्रम बनाए रखते हुए कुंजी और इंडेक्स दोनों द्वारा एक्सेस प्रदान करती है।
OrderedDictionary प्रॉपर्टीज़
| क्रमांक | प्रॉपर्टी | विवरण |
|---|---|---|
| 1 | Count | की-वैल्यू पेयर्स की संख्या प्राप्त करता है। |
| 2 | IsReadOnly | क्या संग्रह रीड-ओनली है? |
| 3 | Item[Int32] | निर्दिष्ट इंडेक्स पर वैल्यू प्राप्त/सेट करता है। |
| 4 | Item[Object] | निर्दिष्ट कुंजी के साथ वैल्यू प्राप्त/सेट करता है। |
| 5 | Keys | कुंजियों का ICollection प्राप्त करता है। |
| 6 | Values | वैल्यूज़ का ICollection प्राप्त करता है। |
OrderedDictionary मेथड्स
| क्रमांक | मेथड | विवरण |
|---|---|---|
| 1 | Add(Object, Object) | निर्दिष्ट कुंजी और मान के साथ एंट्री जोड़ता है। |
| 2 | AsReadOnly() | वर्तमान संग्रह की रीड-ओनली कॉपी लौटाता है। |
| 3 | Clear() | सभी तत्व हटाता है। |
| 4 | Contains(Object) | निर्दिष्ट कुंजी मौजूद है या नहीं जांचता है। |
| 5 | CopyTo(Array, Int32) | तत्वों को एक-आयामी ऐरे में निर्दिष्ट इंडेक्स पर कॉपी करता है (रिवर्स क्रम में)। |
| 6 | Equals(Object) | निर्दिष्ट ऑब्जेक्ट समान है या नहीं (रेफरेंस समानता)। |
| 7 | GetEnumerator() | IDictionaryEnumerator लौटाता है (इंसर्शन क्रम में इटरेट करता है)। |
| 8 | Remove(Object) | निर्दिष्ट कुंजी वाली एंट्री हटाता है। |
| 9 | RemoveAt(Int32) | निर्दिष्ट इंडेक्स पर एंट्री हटाता है। |
उदाहरण 1: Count प्रॉपर्टी और Clear मेथड
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
OrderedDictionary dict = new OrderedDictionary();
dict.Add("A", "Home Appliances");
dict.Add("B", "Electronics");
dict.Add("C", "Smart Wearables");
dict.Add("D", "Pet Supplies");
dict.Add("E", "Clothing");
dict.Add("F", "Footwear");
Console.WriteLine("OrderedDictionary तत्व...");
foreach (DictionaryEntry entry in dict) {
Console.WriteLine(entry.Key + " " + entry.Value);
}
Console.WriteLine("\nतत्वों की संख्या = " + dict.Count);
dict.Clear();
Console.WriteLine("Clear() के बाद संख्या = " + dict.Count);
}
}
आउटपुट
OrderedDictionary तत्व...
A Home Appliances
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
तत्वों की संख्या = 6
Clear() के बाद संख्या = 0
उदाहरण 2: Clear मेथड का एक और उदाहरण
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
OrderedDictionary dict = new OrderedDictionary();
dict.Add("A", "Books");
dict.Add("B", "Electronics");
dict.Add("C", "Smart Wearables");
dict.Add("D", "Pet Supplies");
dict.Add("E", "Clothing");
dict.Add("F", "Footwear");
Console.WriteLine("OrderedDictionary तत्व...");
foreach (DictionaryEntry entry in dict) {
Console.WriteLine(entry.Key + " " + entry.Value);
}
Console.WriteLine("\nतत्वों की संख्या = " + dict.Count);
dict.Clear();
Console.WriteLine("Clear() के बाद संख्या = " + dict.Count);
}
}
आउटपुट
OrderedDictionary तत्व...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
तत्वों की संख्या = 6
Clear() के बाद संख्या = 0
महत्वपूर्ण व्यवहार: CopyTo रिवर्स क्रम
नोट: CopyTo() तत्वों को रिवर्स इंसर्शन क्रम में कॉपी करता है (सबसे हालिया पहले), जबकि foreach और GetEnumerator() इंसर्शन क्रम में इटरेट करते हैं।
dict.Add("1", "One");
dict.Add("2", "Two");
dict.Add("3", "Three");
// foreach क्रम: 1, 2, 3
// CopyTo क्रम: 3, 2, 1
लीगेसी क्लास नोट
OrderedDictionaryनॉन-जेनेरिक है (objectकुंजी/मान)।System.Collections.Specializedनेमस्पेस में (पुराना नेमस्पेस)।- नए कोड के लिए जेनेरिक विकल्प उपयोग करें।
आधुनिक विकल्प
// विकल्प 1: Dictionary + List ऑर्डर के लिए
var dict = new Dictionary<string, string>();
var order = new List<string>();
dict["A"] = "Books"; order.Add("A");
dict["B"] = "Electronics"; order.Add("B");
// विकल्प 2: थर्ड-पार्टी (जैसे OrderedDictionary NuGet पैकेज)
// विकल्प 3: ImmutableDictionary (इम्यूटेबल, ऑर्डर्ड)