परिचय
StringDictionary क्लास एक हैश टेबल को लागू करती है जहां कुंजी और मान दोनों स्ट्रिंग्स के रूप में दृढ़ता से टाइप किए जाते हैं। यह System.Collections.Specialized नेमस्पेस में है।
गुण
| क्र.सं. | गुण और विवरण |
| 1 | Count - कुंजी/मान जोड़े की संख्या। |
| 2 | IsSynchronized - थ्रेड-सेफ है या नहीं। |
| 3 | Item[String] - निर्दिष्ट कुंजी से जुड़ा मान प्राप्त/सेट करता है। |
| 4 | Keys - कुंजियों का संग्रह। |
| 5 | SyncRoot - सिंक्रनाइज़ेशन के लिए ऑब्जेक्ट। |
| 6 | Values - मानों का संग्रह। |
मुख्य मेथड्स
| क्र.सं. | मेथड और विवरण |
| 1 | Add(String, String) - कुंजी और मान के साथ एंट्री जोड़ता है। |
| 2 | Clear() - सभी एंट्रीज़ हटाता है। |
| 3 | ContainsKey(String) - विशिष्ट कुंजी मौजूद है या नहीं जांचता है। |
| 4 | ContainsValue(String) - विशिष्ट मान मौजूद है या नहीं जांचता है। |
| 5 | CopyTo(Array, Int32) - मानों को एक-आयामी सरणी में कॉपी करता है। |
उदाहरण 1: दो StringDictionary ऑब्जेक्ट्स की तुलना
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict1 = new StringDictionary();
strDict1.Add("A", "John");
strDict1.Add("B", "Andy");
strDict1.Add("C", "Tim");
strDict1.Add("D", "Ryan");
strDict1.Add("E", "Kevin");
strDict1.Add("F", "Katie");
strDict1.Add("G", "Brad");
StringDictionary strDict2 = new StringDictionary();
strDict2.Add("A", "John");
strDict2.Add("B", "Andy");
strDict2.Add("C", "Tim");
strDict2.Add("D", "Ryan");
strDict2.Add("E", "Kevin");
strDict2.Add("F", "Katie");
strDict2.Add("G", "Brad");
StringDictionary strDict3 = new StringDictionary();
strDict3 = strDict2;
Console.WriteLine("क्या Dictionary2, Dictionary3 के बराबर है? = " + strDict2.Equals(strDict3));
Console.WriteLine("क्या Dictionary1, Dictionary3 के बराबर है? = " + strDict1.Equals(strDict3));
}
}
आउटपुट
क्या Dictionary2, Dictionary3 के बराबर है? = True
क्या Dictionary1, Dictionary3 के बराबर है? = False
उदाहरण 2: सिंक्रनाइज़ेशन जांच
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict1 = new StringDictionary();
strDict1.Add("A", "John");
strDict1.Add("B", "Andy");
strDict1.Add("C", "Tim");
strDict1.Add("D", "Ryan");
strDict1.Add("E", "Kevin");
strDict1.Add("F", "Katie");
strDict1.Add("G", "Brad");
Console.WriteLine("StringDictionary1 तत्व...");
foreach(DictionaryEntry de in strDict1) {
Console.WriteLine(de.Key + " " + de.Value);
}
StringDictionary strDict2 = new StringDictionary();
strDict2.Add("1", "A");
strDict2.Add("2", "B");
strDict2.Add("3", "C");
strDict2.Add("4", "D");
strDict2.Add("5", "E");
Console.WriteLine("\nStringDictionary2 कुंजी-मान जोड़े...");
IEnumerator demoEnum = strDict2.GetEnumerator();
DictionaryEntry d;
while (demoEnum.MoveNext()) {
d = (DictionaryEntry)demoEnum.Current;
Console.WriteLine("कुंजी = " + d.Key + ", मान = " + d.Value);
}
Console.WriteLine("क्या StringDictionary2 सिंक्रनाइज़्ड है? = " + strDict2.IsSynchronized);
}
}
आउटपुट
StringDictionary1 तत्व...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
StringDictionary2 कुंजी-मान जोड़े...
कुंजी = 1, मान = A
कुंजी = 2, मान = B
कुंजी = 3, मान = C
कुंजी = 4, मान = D
कुंजी = 5, मान = E
क्या StringDictionary2 सिंक्रनाइज़्ड है? = False
मुख्य बिंदु
StringDictionary केस-इनसेंसिटिव कुंजियों का उपयोग करता है (आंतरिक रूप से लोअरकेस में बदल देता है)।
- यह थ्रेड-सेफ नहीं है; सिंक्रनाइज़ेशन के लिए
SyncRoot का उपयोग करें।
- आधुनिक कोड में
Dictionary<string, string> प्राथमिकता दी जाती है।