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

C# में जांचें कि StringDictionary सिंक्रनाइज़्ड है या नहीं

StringDictionary.IsSynchronized गुण इंगित करता है कि StringDictionary थ्रेड-सेफ है या नहीं। मानक StringDictionary इंस्टेंस हमेशा false लौटाते हैं। .NET Framework StringDictionary के लिए कोई अंतर्निहित सिंक्रनाइज़्ड रैपर प्रदान नहीं करता।

उदाहरण 1: दो StringDictionaries

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 elements...");
        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 key-value pairs...");
        IEnumerator demoEnum = strDict2.GetEnumerator();
        DictionaryEntry d;
        while (demoEnum.MoveNext())
        {
            d = (DictionaryEntry)demoEnum.Current;
            Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
        }
        Console.WriteLine("Is the StringDictionary2 synchronized? = " + strDict2.IsSynchronized);
    }
}

आउटपुट

StringDictionary1 elements...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
StringDictionary2 key-value pairs...
Key = 1, Value = A
Key = 2, Value = B
Key = 3, Value = C
Key = 4, Value = D
Key = 5, Value = E
Is the StringDictionary2 synchronized? = False

उदाहरण 2: एक और StringDictionary

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

public class Demo
{
    public static void Main()
    {
        StringDictionary strDict = new StringDictionary();
        strDict.Add("One", "Laptop");
        strDict.Add("Two", "Desktop");
        strDict.Add("Three", "Earphone");
        strDict.Add("Four", "Speaker");
        strDict.Add("Five", "HardDisk");
        strDict.Add("Six", "SSD");
        strDict.Add("Seven", "HardDisk");
        Console.WriteLine("StringDictionary key-value pairs...");
        IEnumerator demoEnum = strDict.GetEnumerator();
        DictionaryEntry d;
        while (demoEnum.MoveNext())
        {
            d = (DictionaryEntry)demoEnum.Current;
            Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
        }
        Console.WriteLine("Is StringDictionary synchronized? = " + strDict.IsSynchronized);
    }
}

आउटपुट

StringDictionary key-value pairs...
Key = five, Value = HardDisk
Key = one, Value = Laptop
Key = four, Value = Speaker
Key = three, Value = Earphone
Key = seven, Value = HardDisk
Key = six, Value = SSD
Key = two, Value = Desktop
Is StringDictionary synchronized? = False

StringDictionary (नामस्थान System.Collections.Specialized) केस-इनसेंसिटिव कुंजियों का उपयोग करता है (आंतरिक रूप से लोअरकेस में बदल देता है)। यह IsSynchronized = false लौटाता है और कोई Synchronized फैक्टरी मेथड प्रदान नहीं करता। थ्रेड सुरक्षा के लिए मैन्युअल लॉकिंग या ConcurrentDictionary<string, string> का उपयोग करें।

  1. - C# में Hashtable केवल पढ़ने योग्य है या नहीं जांचें

    C# में Hashtable केवल पढ़ने योग्य है या नहीं, यह जांचने के लिए IsReadOnly प्रॉपर्टी का उपयोग किया जाता है। यह एक बूलियन मान लौटाता है। साथ ही IsFixedSize प्रॉपर्टी से निश्चित आकार की जांच की जा सकती है। उदाहरण 1 using System; using System.Collections; public class Demo { public static void Main

  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# लिस्ट खाली है या नहीं कैसे चेक करें

    लिस्ट खाली है या नहीं यह जानने के लिए Any मेथड का उपयोग करें। लिस्ट सेट करें - var subjects = new List(); subjects.Add(Maths); subjects.Add(Java); subjects.Add(English); subjects.Add(Science); subjects.Add(Physics); subjects.Add(Chemistry); अब लिस्ट खाली है या नहीं चेक करने के लिए निम्नलिखित कंडीशन