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

C# में StringDictionary को सिंक्रोनाइज़ कैसे करें

StringDictionary को थ्रेड-सेफ बनाने के लिए SyncRoot प्रॉपर्टी के साथ lock स्टेटमेंट का उपयोग करें।

सिंटैक्स

lock(stringDict.SyncRoot) {
    // थ्रेड-सेफ ऑपरेशन्स
}

उदाहरण 1: StringDictionary सिंक्रोनाइज़ेशन

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

public class Demo {
    public static void Main() {
        StringDictionary strDict = new StringDictionary();
        strDict.Add("A", "Books");
        strDict.Add("B", "Electronics");
        strDict.Add("C", "Appliances");
        strDict.Add("D", "Pet Supplies");
        strDict.Add("E", "Clothing");
        strDict.Add("F", "Footwear");
        
        Console.WriteLine("StringDictionary key-value pairs...");
        foreach (DictionaryEntry de in strDict) {
            Console.WriteLine(de.Key + " " + de.Value);
        }
        
        Console.WriteLine("Value associated with key D = " + strDict["D"]);
        Console.WriteLine("Value associated with key F = " + strDict["F"]);
        
        Console.WriteLine("\nSynchronize access..");
        lock (strDict.SyncRoot) {
            foreach (DictionaryEntry de in strDict) {
                Console.WriteLine(de.Key + " " + de.Value);
            }
        }
    }
}

आउटपुट

StringDictionary key-value pairs...
a Books
b Electronics
c Appliances
d Pet Supplies
e Clothing
f Footwear
Value associated with key D = Pet Supplies
Value associated with key F = Footwear
Synchronize access..
a Books
b Electronics
c Appliances
d Pet Supplies
e Clothing
f Footwear

उदाहरण 2: अन्य डेटा के साथ

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

public class Demo {
    public static void Main() {
        StringDictionary strDict = new StringDictionary();
        strDict.Add("A", "John");
        strDict.Add("B", "Andy");
        strDict.Add("C", "Tim");
        strDict.Add("D", "Ryan");
        strDict.Add("E", "Kevin");
        strDict.Add("F", "Katie");
        strDict.Add("G", "Brad");
        
        Console.WriteLine("StringDictionary elements...");
        foreach (DictionaryEntry de in strDict) {
            Console.WriteLine(de.Key + " " + de.Value);
        }
        
        Console.WriteLine("\nSynchronize access..");
        lock (strDict.SyncRoot) {
            foreach (DictionaryEntry de in strDict) {
                Console.WriteLine(de.Key + " " + de.Value);
            }
        }
    }
}

आउटपुट

StringDictionary elements...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
Synchronize access..
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad

महत्वपूर्ण नोट्स

  • StringDictionary थ्रेड-सेफ नहीं है डिफ़ॉल्ट रूप से
  • SyncRoot एक ऑब्जेक्ट लौटाता है जिसका उपयोग लॉकिंग के लिए किया जा सकता है
  • lock स्टेटमेंट यह सुनिश्चित करता है कि एक समय में केवल एक थ्रेड एन्यूमरेट/एक्सेस कर सके
  • केवल एन्यूमरेशन के दौरान लॉक करना पर्याप्त नहीं - सभी एक्सेस (Add, Remove, Indexer) को लॉक करना चाहिए

आधुनिक विकल्प: ConcurrentDictionary

using System.Collections.Concurrent;

// .NET 4.0+ के लिए अनुशंसित - बिल्ट-इन थ्रेड सेफ्टी
var dict = new ConcurrentDictionary<string, string>();
dict.TryAdd("A", "Books");
dict.TryAdd("B", "Electronics");

// लॉक-मुक्त थ्रेड-सेफ ऑपरेशन्स
string value = dict.GetOrAdd("C", "Appliances");
bool removed = dict.TryRemove("A", out _);

StringDictionary vs ConcurrentDictionary

विशेषताStringDictionary + SyncRootConcurrentDictionary
थ्रेड सेफ्टीमैन्युअल लॉकिंग आवश्यकबिल्ट-इन, लॉक-मुक्त
पेरफॉरमेंसलॉक कॉन्टेंशनहाई कॉनकरेंसी के लिए ऑप्टिमाइज़्ड
APIपुराना, नॉन-जेनेरिकजेनेरिक, आधुनिक
एटॉमिक ऑपरेशन्सनहींहाँ (TryAdd, GetOrAdd, आदि)

सिफारिश

नए कोड के लिए ConcurrentDictionary<TKey, TValue> का उपयोग करें। StringDictionary लिगेसी कोड के लिए है।

  1. - C# में StringDictionary के मानों का कलेक्शन प्राप्त करें

    उदाहरण StringDictionary की Keys प्रॉपर्टी कुंजियों का ICollection लौटाती है। मानों को कुंजी के माध्यम से एक्सेस किया जा सकता है। इस उदाहरण में कुंजियों को ऐरे में कॉपी करके कुंजी-मान जोड़े प्रिंट किए गए हैं। using System; using System.Collections; using System.Collections.Specialized; public class

  1. - C# में StringDictionary की कुंजियों का संग्रह प्राप्त करें

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

  1. - Windows 10 और 8 क्विक एक्सेस मेनू (Win+X) को अनुकूलित करें - चरण-दर-चरण मार्गदर्शिका

    क्विक एक्सेस जिसे पावर यूजर मेनू के रूप में भी जाना जाता है, डिवाइस मैनेजर, डिस्क मैनेजमेंट, और कमांड प्रॉम्प्ट जैसे उन्नत सिस्टम टूल्स तक पहुंचने का सबसे तेज़ तरीका है। यह सुविधा Windows 8 के लॉन्च के बाद से पेश की गई है। इसे एक्सेस करने के लिए, आपको Windows और X कुंजी एक साथ दबाने या स्टार्ट मेनू