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 a linked list.
Syntax: public virtual object SyncRoot { get; }
Property Value: An object which can be used to synchronize access to the ListDictionary.
Important Points:
- Synchronization of an object is done so that only one thread can manipulate the data in the ListDictionary.
- 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.
Below programs illustrate the use of the above-discussed property:
Example 1: In this code, we are using SyncRoot to get Synchronized access to the ListDictionary named listdict, which is not a thread-safe procedure and can cause an exception. So to avoid the exception we lock the collection during the enumeration.
// C# program to illustrate the // use of SyncRoot property of // the ListDictionary class using System; using System.Threading; using System.Collections; using System.Collections.Specialized; namespace sync_root { class GFG { // Main Method static void Main(string[] args) { // Declaring an ListDictionary ListDictionary listdict = new ListDictionary(); // Adding elements to ListDictionary listdict.Add("10", "JQUERY"); listdict.Add("20", "HTML"); listdict.Add("30", "CSS"); listdict.Add("40", "C"); listdict.Add("50", "C++"); listdict.Add("60", "C#"); // Using the SyncRoot property lock(listdict.SyncRoot) { // foreach loop to display // the elements in listdict foreach(DictionaryEntry i in listdict) Console.WriteLine(i.Key + " ---- " + i.Value); } } } } |
10 ---- JQUERY 20 ---- HTML 30 ---- CSS 40 ---- C 50 ---- C++ 60 ---- C#
Example 2:
// C# program to illustrate the // use of SyncRoot property of // the ListDictionary class using System; using System.Threading; using System.Collections; using System.Collections.Specialized; namespace sync_root { class GFG { // Main Method static void Main(string[] args) { // Declaring an ListDictionary ListDictionary listdict = new ListDictionary(); // Adding elements to ListDictionary listdict.Add("1", "Ram"); listdict.Add("2", "Shyam"); listdict.Add("3", "Mohan"); listdict.Add("4", "Sohan"); listdict.Add("5", "Rohan"); // Using the SyncRoot property lock(listdict.SyncRoot) { // foreach loop to display // the elements in listdict foreach(DictionaryEntry i in listdict) Console.WriteLine(i.Key + " ----> " + i.Value); } } } } |
1 ----> Ram 2 ----> Shyam 3 ----> Mohan 4 ----> Sohan 5 ----> Rohan
Reference:
Recommended Posts:
- How to get Synchronize access to the Queue in C#
- How to get Synchronize access to the StringDictionary in C#
- How to get Synchronize access to the Stack in C#
- How to get Synchronize access to the Array in C#
- C# | How to get Synchronize access to the ArrayList
- How to get Synchronize access to the HybridDictionary in C#
- How to get Synchronize access to the StringCollection in C#
- C# | Get or set the value associated with specified key in ListDictionary
- C# | Add the specified key and value into the ListDictionary
- How to create a ListDictionary in C#
- C# | ListDictionary Class
- C# | Remove all entries from the ListDictionary
- C# | Check if ListDictionary is read-only
- C# | Check if ListDictionary contains a specific key
- C# | Get an enumerator that iterates through the ListDictionary
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.


