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 than objects.
Syntax: public virtual object SyncRoot { get; }
Property Value: An object which can be used to synchronize access to the StringDictionary.
Important Points:
- Synchronization of an object is done so that only one thread can manipulate the data in the StringDictionary.
- 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 StringDictionary named sd, 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 StringDictionary 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 StringDictionary StringDictionary sd = new StringDictionary(); // Adding elements to StringDictionary sd.Add("Australia", "Canberra"); sd.Add("Belgium", "Brussels"); sd.Add("Netherlands", "Amsterdam"); sd.Add("China", "Beijing"); sd.Add("Russia", "Moscow"); sd.Add("India", "New Delhi"); // Using the SyncRoot property lock(sd.SyncRoot) { // foreach loop to display // the elements in sd foreach(DictionaryEntry i in sd) Console.WriteLine(i.Key + " ---- " + i.Value); } } } } |
china ---- Beijing netherlands ---- Amsterdam belgium ---- Brussels australia ---- Canberra india ---- New Delhi russia ---- Moscow
Example 2:
// C# program to illustrate the // use of SyncRoot property of // the StringDictionary 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 StringDictionary StringDictionary sd = new StringDictionary(); // Adding elements to StringDictionary sd.Add("1", "C"); sd.Add("2", "C++"); sd.Add("3", "Java"); sd.Add("4", "C#"); sd.Add("5", "HTML"); // Using the SyncRoot property lock(sd.SyncRoot) { // foreach loop to display // the elements in sd foreach(DictionaryEntry i in sd) Console.WriteLine(i.Key + " ----> " + i.Value); } } } } |
3 ----> Java 5 ----> HTML 4 ----> C# 2 ----> C++ 1 ----> C
Reference:
Recommended Posts:
- How to get Synchronize access to the ListDictionary in C#
- How to get Synchronize access to the Stack in C#
- 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 HybridDictionary in C#
- How to get Synchronize access to the Array in C#
- How to get Synchronize access to the StringCollection in C#
- C# | Get or set the value associated with the specified key in StringDictionary
- C# | Add key and value into StringDictionary
- C# | StringDictionary Class
- C# | Gets or sets the value at the specified key in StringDictionary
- How to create a StringDictionary in C#
- C# | Get the number of key/value pairs in the StringDictionary
- C# | Remove entry with specified key from the StringDictionary
- C# | Check if the StringDictionary contains a specific key
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.


