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 collection is large.
Syntax: public virtual object SyncRoot { get; }
Property Value: An object which can be used to synchronize access to the HybridDictionary.
Important Points:
- Synchronization of an object is done so that only one thread can manipulate the data in the HybridDictionary.
- 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 HybridDictionary named hd, 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 HybridDictionary 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 HybridDictionary HybridDictionary hd = new HybridDictionary(); // Adding elements to HybridDictionary hd.Add("1", "HTML"); hd.Add("2", "CSS"); hd.Add("3", "PHP"); hd.Add("4", "JQuery"); hd.Add("5", "JavaScript"); // Using the SyncRoot property lock(hd.SyncRoot) { // foreach loop to display // the elements in hd foreach(DictionaryEntry i in hd) Console.WriteLine(i.Key + " ---- " + i.Value); } } } } |
1 ---- HTML 2 ---- CSS 3 ---- PHP 4 ---- JQuery 5 ---- JavaScript
Example 2:
// C# program to illustrate the // use of SyncRoot property of // the HybridDictionary 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 HybridDictionary HybridDictionary hd= new HybridDictionary(); // Adding elements to HybridDictionary hd.Add("10", "Shyam"); hd.Add("20", "Ram"); hd.Add("30", "Rohit"); hd.Add("40", "Sohan"); hd.Add("50", "Rohan"); // Using the SyncRoot property lock(hd.SyncRoot) { // foreach loop to display // the elements in hd foreach(DictionaryEntry i in hd) Console.WriteLine(i.Key + " ----> " + i.Value); } } } } |
10 ----> Shyam 20 ----> Ram 30 ----> Rohit 40 ----> Sohan 50 ----> Rohan
Reference:
Recommended Posts:
- C# | How to get Synchronize access to the ArrayList
- How to get Synchronize access to the Queue in C#
- How to get Synchronize access to the Stack in C#
- How to get Synchronize access to the ListDictionary in C#
- How to get Synchronize access to the StringCollection in C#
- How to get Synchronize access to the StringDictionary in C#
- How to get Synchronize access to the Array in C#
- C# | Gets or sets the value in HybridDictionary with specified key
- C# | Adding the specified key and value into HybridDictionary
- C# | HybridDictionary Class
- C# | Check the HybridDictionary for a specific key
- C# | Check if HybridDictionary is read only
- C# | Get an enumerator that iterates through the HybridDictionary
- C# | Removing all entries from HybridDictionary
- C# | Removing the specified key entry from HybridDictionary
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.


