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

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

SortedList.IsSynchronized गुण इंगित करता है कि SortedList थ्रेड-सेफ है या नहीं। मानक SortedList false लौटाता है। सिंक्रनाइज़्ड रैपर के लिए SortedList.Synchronized(SortedList) स्थैतिक मेथड का उपयोग करें।

उदाहरण 1: सिंक्रनाइज़्ड रैपर बनाना

using System;
using System.Collections;

public class Demo
{
    public static void Main()
    {
        SortedList list = new SortedList();
        list.Add("1", "One");
        list.Add("2", "Two");
        list.Add("3", "Three");
        list.Add("4", "Four");
        list.Add("5", "Five");
        list.Add("6", "Six");
        list.Add("7", "Seven");
        list.Add("8", "Eight");
        Console.WriteLine("Key and Value of SortedList....");
        foreach (DictionaryEntry k in list)
            Console.WriteLine("Key: {0}, Value: {1}", k.Key, k.Value);
        Console.WriteLine("Is the SortedList having the value? " + list.ContainsValue("Five"));
        SortedList sortedList = SortedList.Synchronized(list);
        Console.WriteLine("Is the SortedList synchronized? = " + sortedList.IsSynchronized);
    }
}

आउटपुट

Key and Value of SortedList....
Key: 1, Value: One
Key: 2, Value: Two
Key: 3, Value: Three
Key: 4, Value: Four
Key: 5, Value: Five
Key: 6, Value: Six
Key: 7, Value: Seven
Key: 8, Value: Eight
Is the SortedList having the value? True
Is the SortedList synchronized? = True

उदाहरण 2: असिंक्रनाइज़्ड SortedList

using System;
using System.Collections;

public class Demo
{
    public static void Main()
    {
        SortedList list = new SortedList();
        list.Add("1", "One");
        list.Add("2", "Two");
        list.Add("3", "Three");
        list.Add("4", "Four");
        list.Add("5", "Five");
        list.Add("6", "Six");
        list.Add("7", "Seven");
        list.Add("8", "Eight");
        Console.WriteLine("Key and Value of SortedList....");
        foreach (DictionaryEntry k in list)
            Console.WriteLine("Key: {0}, Value: {1}", k.Key, k.Value);
        Console.WriteLine("Is the SortedList having the value? " + list.ContainsValue("Ten"));
        Console.WriteLine("Is the SortedList synchronized? = " + list.IsSynchronized);
    }
}

आउटपुट

Key and Value of SortedList....
Key: 1, Value: One
Key: 2, Value: Two
Key: 3, Value: Three
Key: 4, Value: Four
Key: 5, Value: Five
Key: 6, Value: Six
Key: 7, Value: Seven
Key: 8, Value: Eight
Is the SortedList having the value? False
Is the SortedList synchronized? = False

SortedList.Synchronized एक थ्रेड-सेफ रैपर बनाता है जो सभी सार्वजनिक मेथड्स पर लॉक करता है। एन्यूमरेशन के लिए मैन्युअल lock(list.SyncRoot) आवश्यक है। ContainsValue O(n) रैखिक खोज करता है। आधुनिक कोड में SortedDictionary<TKey, TValue> (कोई सिंक्रनाइज़्ड रैपर नहीं) या ConcurrentDictionary पसंद किया जाता है।

  1. - C# में जांचें कि SortedList ऑब्जेक्ट का आकार निश्चित है या नहीं

    उदाहरण SortedList की IsFixedSize प्रॉपर्टी बताती है कि सूची का आकार निश्चित है या नहीं। यह प्रॉपर्टी हमेशा false लौटाती है क्योंकि SortedList गतिशील रूप से आकार बदल सकती है। using System; using System.Collections; public class Demo { public static void Main() { SortedList list = new Sor

  1. - C# में जांचें कि SortedList ऑब्जेक्ट में विशिष्ट मान है या नहीं

    यह जांचने के लिए कि क्या SortedList ऑब्जेक्ट में कोई विशिष्ट मान है, कोड इस प्रकार है: उदाहरण using System; using System.Collections; public class Demo { public static void Main(){ SortedList list = new SortedList(); list.Add(1, One); list.Add(2, Two); lis

  1. - पायथन – जांचें कि K की के लिए कोई विशेष मान मौजूद है या नहीं

    जब यह जांचना हो कि K की के लिए कोई विशेष मान मौजूद है या नहीं, तो लिस्ट कॉम्प्रिहेंशन का उपयोग किया जाता है।उदाहरणmy_list = [{python : 14, is : great, fun : 1`},{python : cool, is : fun, best : 81},{python : 93, is : CS, amazing : 16}]print(The list is :)print(my_list)K = pythonprint(The value of K