Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C#

C# में StringDictionary में किसी विशिष्ट कुंजी की जांच कैसे करें

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
  1. - C# में Hashtable में किसी विशिष्ट मान की जांच कैसे करें

    C# में Hashtable किसी विशिष्ट मान को रखता है या नहीं, यह जांचने के लिए ContainsValue() मेथड का उपयोग किया जाता है। यह मेथड एक बूलियन मान लौटाता है। उदाहरण 1 using System; using System.Collections; public class Demo { public static void Main() { Hashtable hash = new Hashtable();

  1. - C# में Collection में किसी तत्व की जांच कैसे करें

    C# में Collection<T> किसी विशिष्ट तत्व को रखता है या नहीं, यह जांचने के लिए Contains() मेथड का उपयोग किया जाता है। यह मेथड एक बूलियन मान लौटाता है। उदाहरण 1 using System; using System.Collections.ObjectModel; public class Demo { public static void Main() { Collection col = new Col

  1. - C# में HashSet में निर्दिष्ट तत्व है या नहीं जांचें

    C# में HashSet किसी विशिष्ट तत्व को रखता है या नहीं, यह जांचने के लिए Contains() मेथड का उपयोग किया जाता है। यह मेथड एक बूलियन मान लौटाता है। उदाहरण 1 using System; using System.Collections.Generic; public class Demo { public static void Main() { HashSet set1 = new HashSet(); se