C# | How to get Synchronize access to the ArrayList - GeeksforGeeks

18 Feb, 2019
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, searching and sorting items in the list.
Important Points:
Synchronization of an object is done so that only one thread can manipulate the data in the ArrayList.
A property is a member of a class that provides a means for reading, writing and computing private data fields.
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: public virtual object SyncRoot { get; }
Property Value: An object that can be used to synchronize access to the ArrayList.
Example 1: In this code, we are using SyncRoot to get Synchronized access to the ArrayList named arrlist, which is not a thread-safe procedure and can cause an exception. So to avoid the exception we lock the collection.
// C# program to illustrate the// use of SyncRoot property of// the ArrayListusing System;using System.Threading;using System.Collections;  namespace sync_root {  class GFG {      // Main Method    static void Main(string[] args)    {          // Declaring an ArrayList        ArrayList arrlist = new ArrayList();          // Adding elements to ArrayList        arrlist.Add(1);        arrlist.Add(2);        arrlist.Add(3);        arrlist.Add(4);        arrlist.Add(5);          // Using the SyncRoot property        lock(arrlist.SyncRoot)        {            // foreach loop to display            // the elements in arrlist            foreach(Object i in arrlist)                Console.WriteLine(i);        }    }}}
1
2
3
4
5

Example 2:
// C# program to illustrate the// use of SyncRoot property of// the ArrayListusing System;using System.Threading;using System.Collections;  namespace sync_root {  class GFG {      // Main Method    static void Main(string[] args)    {          // Declaring an ArrayList        ArrayList arrlist = new ArrayList();          // Adding elements to ArrayList        arrlist.Add("C");        arrlist.Add("C++");        arrlist.Add("Java");        arrlist.Add("C#");        arrlist.Add("HTML");          // Using the SyncRoot property        lock(arrlist.SyncRoot)        {            // foreach loop to display            // the elements in arrlist            foreach(Object i in arrlist)                Console.WriteLine(i);        }    }}}
C
C++
Java
C#
HTML

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist.syncroot?view=netframework-4.7.2
CSharp-Collections-ArrayList
CSharp-Collections-Namespace
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# Dictionary with examples
C# | Method Overriding
Destructors in C#
Difference between Ref and Out keywords in C#
C# | Delegates
C# | String.IndexOf( ) Method | Set - 1
C# | Constructors
Extension Method in C#
Introduction to .NET Framework
C# | Class and Object