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

C# में StringDictionary में कुंजी-मान जोड़े की संख्या प्राप्त करें

C# में StringDictionary वर्ग की Count प्रॉपर्टी का उपयोग करके डिक्शनरी में मौजूद कुंजी-मान जोड़े की कुल संख्या प्राप्त की जा सकती है। यह प्रॉपर्टी एक पूर्णांक मान लौटाती है जो संग्रहीत प्रविष्टियों की संख्या दर्शाता है।

उदाहरण 1: Count प्रॉपर्टी का उपयोग

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
    public static void Main() {
        StringDictionary strDict = new StringDictionary();
        strDict.Add("1", "One");
        strDict.Add("2", "Two");
        strDict.Add("3", "Three");
        strDict.Add("4", "Four");
        
        Console.WriteLine("StringDictionary कुंजी-मान जोड़े...");
        
        IEnumerator demoEnum = strDict.GetEnumerator();
        DictionaryEntry d;
        
        while (demoEnum.MoveNext()) {
            d = (DictionaryEntry)demoEnum.Current;
            Console.WriteLine("कुंजी = " + d.Key + ", मान = " + d.Value);
        }
        
        Console.WriteLine("StringDictionary कुंजी-मान जोड़ों की संख्या..." + strDict.Count);
    }
}

आउटपुट

StringDictionary कुंजी-मान जोड़े...
कुंजी = 1, मान = One
कुंजी = 2, मान = Two
कुंजी = 3, मान = Three
कुंजी = 4, मान = Four
StringDictionary कुंजी-मान जोड़ों की संख्या...4

उदाहरण 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);
        }
        Console.WriteLine("StringDictionary1 कुंजी-मान जोड़ों की संख्या..." + strDict1.Count);
        
        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.Count);
    }
}

आउटपुट

StringDictionary1 तत्व...
A John
B Andy
C Tim
D Ryan
E Kevin
F Katie
G Brad
StringDictionary1 कुंजी-मान जोड़ों की संख्या...7

StringDictionary2 कुंजी-मान जोड़े...
कुंजी = 1, मान = A
कुंजी = 2, मान = B
कुंजी = 3, मान = C
कुंजी = 4, मान = D
कुंजी = 5, मान = E
StringDictionary2 कुंजी-मान जोड़ों की संख्या...5

मुख्य बिंदु

  • Count प्रॉपर्टी केवल पढ़ने योग्य है
  • यह int मान लौटाती है
  • StringDictionary केवल स्ट्रिंग कुंजी और मान स्वीकार करता है
  • कुंजियाँ केस-असंवेदनशील होती हैं
  1. - C# में StringDictionary की कुंजियों का संग्रह प्राप्त करें

    परिचय StringDictionary क्लास (System.Collections.Specialized नेमस्पेस में) एक हैश टेबल है जिसमें कुंजी और मान दोनों स्ट्रिंग होते हैं। इसकी Keys प्रॉपर्टी कुंजियों का ICollection लौटाती है, जिसे ऐरे में कॉपी किया जा सकता है या एन्यूमरेट किया जा सकता है। उदाहरण 1: कुंजियों को ऐरे में कॉपी करना using

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

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

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

    C# में StringDictionary किसी विशिष्ट कुंजी को रखता है या नहीं, यह जांचने के लिए ContainsKey() मेथड का उपयोग किया जाता है। यह मेथड एक बूलियन मान लौटाता है: true यदि कुंजी मौजूद है, अन्यथा false। उदाहरण 1 using System; using System.Collections; using System.Collections.Specialized; public class Demo