C# | Creating a synchronized (thread-safe) wrapper for a SortedList object Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report SortedList.Synchronized(SortedList) Method is used to get the synchronized (thread-safe) wrapper for a SortedList object. Syntax: public static System.Collections.SortedList Synchronized (System.Collections.SortedList list); Here, list is the name of the SortedList object which is to be synchronized. Exception: This method will throw ArgumentNullException if the list is null. Below programs illustrate the use of above-discussed method: Example 1: CSharp // C# code to create a synchronized // (thread-safe) wrapper for a // SortedList object 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); // --------- Using IsSynchronized Property // print the status of both SortedList Console.WriteLine("mySortedList SortedList is {0}.", mySortedList.IsSynchronized ? "synchronized" : "not synchronized"); Console.WriteLine("mySortedList_1 SortedList is {0}.", mySortedList_1.IsSynchronized ? "synchronized" : "not synchronized"); } } Output: mySortedList SortedList is not synchronized. mySortedList_1 SortedList is synchronized. Example 2: CSharp // C# code to create a synchronized // (thread-safe) wrapper for a // SortedList object using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating an SortedList SortedList mySortedList = new SortedList(); // Adding elements in SortedList mylist.Add("4", "Even"); mylist.Add("9", "Odd"); mylist.Add("5", "Odd and Prime"); mylist.Add("2", "Even and Prime"); // it will give runtime error as // table parameter can't be null SortedList my2 = SortedList.Synchronized(null); } } Runtime Error: Unhandled Exception: System.ArgumentNullException: Value cannot be null. Parameter name: list Note: For the thread safety of a SortedList object, all operations must be done through this wrapper only. Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.sortedlist.synchronized?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Creating a synchronized (thread-safe) wrapper for a SortedList object K Kirti_Mangal Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Collections-SortedList Similar Reads C# | Creating a synchronized (thread-safe) wrapper for the Hashtable Hashtable.Synchronized(Hashtable) Method is used to return a synchronized (thread-safe) wrapper for the Hashtable.Syntax: public static System.Collections.Hashtable Synchronized (System.Collections.Hashtable table); Here table is the Hashtable which is to be synchronized.Return Value: This method re 2 min read C# | Creating a synchronized (thread-safe) wrapper for the ArrayList Synchronized(ArrayList) method is used to get an ArrayList wrapper that is synchronized (thread safe). Syntax: public static System.Collections.ArrayList Synchronized (System.Collections.ArrayList list); Here, the list is the ArrayList which is to be synchronized. Return Value: It returns an ArrayLi 2 min read 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 ac 2 min read C# | Check if an array is synchronized (thread safe) or not Array.IsSynchronized Property is used to get a value which indicates whether access to the Array is synchronized (thread-safe) or not. Syntax: public bool IsSynchronized { get; } Property Value: This property always returns false for all arrays. Below programs illustrate the use of above-discussed p 2 min read C# | Check if ArrayList is Synchronized (thread safe) ArrayList.IsSynchronized Property is used to get a value which indicate whether access to the ArrayList is synchronized (thread safe). Syntax: public virtual bool IsSynchronized { get; } Return Value: This property returns the true if access to the ArrayList is synchronized (thread safe) otherwise i 2 min read C# | Check if Hashtable is synchronized (thread safe) Hashtable.IsSynchronized Property is used to get a value indicating whether access to the Hashtable is synchronized(thread-safe). Syntax: public virtual bool IsSynchronized { get; } Return Value: This property return true if access to the Hashtable is synchronized (thread-safe), otherwise it returns 2 min read C# | How to get Synchronize access to the ArrayList ArrayList.SyncRoot Property is used to get an object which can be used to synchronize access to the ArrayList. ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searc 2 min read How to get Synchronize access to the Queue in C# Queue.SyncRoot Property is used to get an object which can be used to synchronize access to the Queue. Queue represents a first-in, first out collection of object. It is used when you need first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remov 2 min read Stack.Synchronized() Method in C# This method(comes under System.Collections Namespace) is used to return a synchronized (thread safe) wrapper for the Stack. To guarantee the thread safety of the Stack, all operations must be done through this wrapper. Syntax: public static System.Collections.Stack Synchronized (System.Collections.S 2 min read Queue.Synchronized() Method in C# This method is used to return a new Queue that wraps the original queue and is thread-safe. The wrapper returned by this method locks the queue before an operation is performed so that it is performed in a thread-safe manner. Syntax: public static System.Collections.Queue Synchronized (System.Collec 2 min read Like