How to get Synchronize access to the StringDictionary in C# (original) (raw)
Last Updated : 21 Feb, 2019
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.
csharp `
// 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);
}
}
} }
`
Output:
china ---- Beijing netherlands ---- Amsterdam belgium ---- Brussels australia ---- Canberra india ---- New Delhi russia ---- Moscow
Example 2:
csharp `
// 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);
}
}
} }
`
Output:
3 ----> Java 5 ----> HTML 4 ----> C# 2 ----> C++ 1 ----> C
Reference:
Similar Reads
- 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 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
- 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 Array in C# 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 o 3 min read
- 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
- C# | Check if StringDictionary is synchronized (thread safe) StringDictionary.IsSynchronized property is used to get a value indicating whether access to the StringDictionary is synchronized (thread-safe). Syntax: public virtual bool IsSynchronized { get; } Return Value: This method returns True if access to the StringDictionary is synchronized (thread safe), 1 min read
- How to create a StringDictionary in C# StringDictionary() constructor is used to initialize a new instance of the StringDictionary class which will be empty and will have the default initial capacity. StringDictionary is a specialized collection. This class comes under the System.Collections.Specialized namespace. It only allows string k 2 min read
- C# | Check if ListDictionary is synchronized (thread safe) ListDictionary.IsSynchronized property is used to get a value indicating whether the ListDictionary is synchronized (thread safe) or not. Syntax: public bool IsSynchronized { get; } Return Value: This property always returns false. Below are the programs to illustrate the use of ListDictionary.IsSyn 1 min read