How to get Synchronize access to the Array in C# Last Updated : 05 Feb, 2019 Comments Improve Suggest changes Like Article Like Report Array.SyncRoot Property is used to get an object that can be used to synchronize access to the Array. An array is a group of like-typed variables that are referred to by a common name. Array class comes under the System namespace. Important Points: Synchronization of an object is done so that only one thread can manipulate the data in the array. A property is a member of a class that provides a means for reading, writing and computing private data fields. Array.SyncRoot Property implements the System.Collections.ICollection interface. The synchronizing code cannot directly perform on the collection, so it must perform operations on the SyncRoot of the collection in order to guarantee the proper operation of collections that are derived from other objects. Retrieving the value of this property is an O(1) operation. Syntax: arrayName.SyncRoot Property Value: An object that can be used to synchronize access to the Array. Example 1: In this code, we are using SyncRoot to get Synchronized access to the Array named arr, which is not a thread-safe procedure and can cause an exception. So to avoid the exception we lock the collection during the enumeration. csharp // C# program to illustrate the // use of SyncRoot property using System; using System.Threading; using System.Collections; namespace sync_root { class GFG { // Main Method static void Main(string[] args) { // Declaring an array Array arr = new int[] {12, 15, 20, 3, 6}; // Using the SyncRoot property lock(arr.SyncRoot) { // foreach loop to display // the elements in arr foreach(Object i in arr) Console.WriteLine(i); } } } } Output: 12 15 20 3 6 Example 2: In the below example we use SynRoot property with an array of objects of class X. csharp // C# program to depict the use // of SyncRoot Property // for an array of objects using System; using System.Threading; using System.Collections; namespace sync_root_2 { // User defined class X class X { int x; // defining Constructor public X() { x = 0; } // Method to get the value // stored in x public int getValue() { return x; } } // Driver Class public class Program { // Main Method static void Main(string[] args) { // Declaring an array of objects // of type class X X[] obj = new X[5]; // Calling the constructor // for each object for (int i = 0; i < 5; i++) obj[i] = new X(); // Synchronizing the array lock(obj.SyncRoot) { // Displaying the value foreach(X element in obj) Console.WriteLine(element.getValue()); } } } } Output: 0 0 0 0 0 Reference: https://docs.microsoft.com/en-us/dotnet/api/system.array.syncroot?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article How to get Synchronize access to the Array in C# M ManasiKirloskar Follow Improve Article Tags : C# CSharp-Arrays Similar Reads 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 Stack in C# Stack.SyncRoot Property is used to get an object which can be used to synchronize access to the Stack. Stack represents last-in, first out collection of object. It is used when you need last-in, first-out access to items. When you add an item in the list, it is called pushing the item and when you r 3 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 How to get Synchronize access to the ListDictionary in C# ListDictionary.SyncRoot Property is used to get an object which can be used to synchronize access to the ListDictionary. ListDictionary is a specialized collection. It comes under the System.Collections.Specialized namespace. This type represents a non-generic dictionary type. It is implemented with 3 min read How to get Synchronize access to the StringCollection in C# StringCollection.SyncRoot Property is used to get an object which can be used to synchronize access to the StringCollection. This class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace. Syntax: public virtual object Sy 2 min read How to get Synchronize access to the StringDictionary in C# StringDictionary.SyncRoot Property is used to get an object which can be used to synchronize access to the StringDictionary. It only allows string keys and string values. It suffers from performance problems. It implements a hash table with the key and the value strongly typed to be strings rather t 3 min read How to get Synchronize access to the HybridDictionary in C# HybridDictionary.SyncRoot Property is used to get an object which can be used to synchronize access to the HybridDictionary. It implements a linked list and hash table data structure. It implements IDictionary by using a ListDictionary when the collection is small, and a Hashtable when the collectio 3 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 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 the BitArray is synchronized (thread safe) The BitArray class manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on i.e, 1 and false indicates the bit is off i.e, 0. This class is contained in System.Collections namespace. BitArray.IsSynchronized property is used to get a value indi 2 min read Like