Open In App

C# | Check if a SortedList object is synchronized

SortedList class is a collection of (key, value) pairs which are sorted according to keys. Those pairs can be accessible by key and as well as by index(zero-based indexing). This comes under System.Collections namespace. SortedList.IsSynchronized property is used to get a value indicating whether access to a SortedList object is synchronized (thread safe) or not.

Properties:

Syntax:

public virtual bool IsSynchronized { get; }

Return Value: This method returns True if the access to the SortedList object is synchronized (thread safe) otherwise this method returns False. The default value is False.

Below programs illustrate the use of SortedList.IsSynchronized Property:

Example 1:




// C# code to check if a SortedList
// object is synchronized (thread safe)
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating an SortedList
        SortedList mySortedList = new SortedList();
  
        // Checking if a SortedList object
        // is synchronized (thread safe) or not
        Console.WriteLine(mySortedList.IsSynchronized);
    }
}

Output:
False

Example 2:




// C# code to check if a SortedList
// object is synchronized (thread safe)
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating an SortedList
        SortedList mySortedList = new SortedList();
  
        // Adding elements to SortedList
        mySortedList.Add("1", "one");
        mySortedList.Add("2", "two");
        mySortedList.Add("3", "three");
        mySortedList.Add("4", "four");
        mySortedList.Add("5", "five");
  
        // Creating a synchronized wrapper
        // around the SortedList.
        SortedList mySortedList_1 = SortedList.Synchronized(mySortedList);
  
        // Checking if a SortedList object
        // is synchronized (thread safe) or not
        Console.WriteLine(mySortedList_1.IsSynchronized);
    }
}

Output:
True

Reference:


Article Tags :
C#