उदाहरण
HybridDictionary कंस्ट्रक्टर एक caseInsensitive बूलियन पैरामीटर स्वीकार करता है। जब false पास किया जाता है, तो डिक्शनरी केस-सेंसिटिव होती है (यानी "A" और "a" अलग-अलग कुंजियाँ हैं)।
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
// केस-सेंसिटिव HybridDictionary बनाएं (caseInsensitive = false)
HybridDictionary myDict = new HybridDictionary(5, false);
myDict.Add("A", "AB");
myDict.Add("B", "BC");
myDict.Add("C", "DE");
myDict.Add("D", "FG");
myDict.Add("e", "fg"); // छोटा 'e' अलग कुंजी है
Console.WriteLine("की-वैल्यू जोड़े...");
foreach(DictionaryEntry de in myDict)
Console.WriteLine("Key = " + de.Key + ", Value = " + de.Value);
}
}
आउटपुट
की-वैल्यू जोड़े... Key = A, Value = AB Key = B, Value = BC Key = C, Value = DE Key = D, Value = FG Key = e, Value = fg
एक और उदाहरण
जब caseInsensitive = true (डिफ़ॉल्ट) होता है, तो "e" और "E" को एक ही कुंजी माना जाता है, जिससे डुप्लीकेट कुंजी अपवाद होता है।
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
// केस-इनसेंसिटिव HybridDictionary (डिफ़ॉल्ट व्यवहार)
HybridDictionary myDict = new HybridDictionary(5, true);
myDict.Add("A", "AB");
myDict.Add("B", "BC");
myDict.Add("C", "DE");
myDict.Add("e", "fg");
myDict.Add("E", "FG"); // अपवाद फेंकेगा क्योंकि "e" पहले से मौजूद है
Console.WriteLine("की-वैल्यू जोड़े...");
foreach(DictionaryEntry de in myDict)
Console.WriteLine("Key = " + de.Key + ", Value = " + de.Value);
}
}
आउटपुट
रन-टाइम अपवाद: An entry with the same key already exists. Stack Trace: [System.ArgumentException: An entry with the same key already exists.] at System.Collections.Specialized.ListDictionary.Add(Object key, Object value) at System.Collections.Specialized.HybridDictionary.Add(Object key, Object value) at Demo.Main() :line 15