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

C# में StringDictionary क्लास: गुण, मेथड्स और उदाहरण

परिचय

StringDictionary क्लास एक हैश टेबल को लागू करती है जहां कुंजी और मान दोनों स्ट्रिंग्स के रूप में दृढ़ता से टाइप किए जाते हैं। यह System.Collections.Specialized नेमस्पेस में है।

गुण

क्र.सं.गुण और विवरण
1Count - कुंजी/मान जोड़े की संख्या।
2IsSynchronized - थ्रेड-सेफ है या नहीं।
3Item[String] - निर्दिष्ट कुंजी से जुड़ा मान प्राप्त/सेट करता है।
4Keys - कुंजियों का संग्रह।
5SyncRoot - सिंक्रनाइज़ेशन के लिए ऑब्जेक्ट।
6Values - मानों का संग्रह।

मुख्य मेथड्स

क्र.सं.मेथड और विवरण
1Add(String, String) - कुंजी और मान के साथ एंट्री जोड़ता है।
2Clear() - सभी एंट्रीज़ हटाता है।
3ContainsKey(String) - विशिष्ट कुंजी मौजूद है या नहीं जांचता है।
4ContainsValue(String) - विशिष्ट मान मौजूद है या नहीं जांचता है।
5CopyTo(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> प्राथमिकता दी जाती है।
  1. C# में Console क्लास C# में Console क्लास

    C# में Console क्लास का उपयोग कंसोल एप्लिकेशन के लिए मानक इनपुट, आउटपुट और त्रुटि स्ट्रीम को दर्शाने के लिए किया जाता है। आइए C# में Console क्लास प्रॉपर्टीज के कुछ उदाहरण देखते हैं: Console.CursorLeft प्रॉपर्टी C# में कंसोल का CursorLeft बदलने के लिए Console.CursorLeft प्रॉपर्टी का उपयोग करें। उदा

  1. - C# में ArrayList क्लास का Capacity गुण क्या है?

    ArrayList क्लास में Capacity गुण उन एलिमेंट्स की संख्या को प्राप्त या सेट करता है जिन्हें ArrayList समाहित कर सकता है।Capacity हमेशा Count से अधिक होती है। Capacity गुण के लिए:arrList.Capacityडिफ़ॉल्ट क्षमता 4 होती है। यदि 5 एलिमेंट्स हैं, तो इसकी क्षमता दोगुनी होकर 8 हो जाएगी। यह क्रम चलता रहता है।

  1. - C# में क्लास: परिभाषा, संरचना और उदाहरण

    C# में क्लास एक डेटा टाइप का ब्लूप्रिंट (खाका) होता है। यह ऑब्जेक्ट्स बनाने के लिए एक टेम्पलेट की तरह काम करता है। क्लास के अंदर परिभाषित वेरिएबल्स (फील्ड्स) और मेथड्स (फंक्शन्स) को मिलाकर क्लास के मेंबर्स कहा जाता है। क्लास का सामान्य सिंटैक्स C# में क्लास घोषित करने का सामान्य रूप निम्नलिखित है: