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 Like