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:
- A SortedList element can be accessed by its key or by its index.
- A SortedList object internally maintains two arrays to store the elements of the list, i.e, one array for the keys and another array for the associated values.
- A key cannot be null, but a value can be.
- The capacity of a SortedList object is the number of elements the SortedList can hold.
- A SortedList does not allow duplicate keys.
- Operations on a SortedList object tend to be slower than operations on a Hashtable object because of the sorting.
- Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.
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); } } |
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); } } |
True
Reference:
Recommended Posts:
- C# | Creating a synchronized (thread-safe) wrapper for a SortedList object
- C# | Check whether a SortedList object contains a specific key
- C# | Check if a SortedList object contains a specific value
- C# | Check if a SortedList object has a fixed size
- C# | Getting the value at the specified index of a SortedList object
- C# | Getting the key at the specified index of a SortedList object
- C# | Getting the Values in a SortedList object
- C# | Getting the index of the specified key in a SortedList object
- C# | Getting the keys in a SortedList object
- C# | Getting index of the specified value in a SortedList object
- C# | Search in a SortedList object
- C# | Getting the list of Values of a SortedList object
- C# | Getting the list of keys of a SortedList object
- C# | Replacing the value at a specific index in a SortedList object
- C# | Copying the SortedList elements to an Array Object
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.