C# में StringDictionary किसी विशिष्ट कुंजी को रखता है या नहीं, यह जांचने के लिए ContainsKey() मेथड का उपयोग किया जाता है। यह मेथड एक बूलियन मान लौटाता है: true यदि कुंजी मौजूद है, अन्यथा false।
उदाहरण 1
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 d in strDict1) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("क्या StringDictionary1 में कुंजी G है? " + strDict1.ContainsKey("G"));
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");
Console.WriteLine("\nStringDictionary2 तत्व...");
foreach (DictionaryEntry d in strDict2) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("\nक्या Dictionary2, Dictionary1 के बराबर है? = " + strDict2.Equals(strDict1));
}
}
आउटपुट
StringDictionary1 तत्व... a John b Andy c Tim d Ryan e Kevin f Katie g Brad क्या StringDictionary1 में कुंजी G है? True StringDictionary2 तत्व... a John b Andy c Tim d Ryan e Kevin f Katie g Brad क्या Dictionary2, Dictionary1 के बराबर है? = 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 d in strDict1) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("क्या StringDictionary1 में कुंजी G है? " + strDict1.ContainsKey("G"));
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");
Console.WriteLine("\nStringDictionary2 तत्व...");
foreach (DictionaryEntry d in strDict2) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("\nक्या Dictionary2, Dictionary1 के बराबर है? = " + strDict2.Equals(strDict1));
Console.WriteLine("क्या StringDictionary2 में कुंजी B है? " + strDict2.ContainsKey("B"));
}
}
आउटपुट
StringDictionary1 तत्व... a John b Andy c Tim d Ryan e Kevin f Katie g Brad क्या StringDictionary1 में कुंजी G है? True StringDictionary2 तत्व... a John b Andy c Tim d Ryan e Kevin f Katie g Brad क्या Dictionary2, Dictionary1 के बराबर है? = False क्या StringDictionary2 में कुंजी B है? True