C# | Check if Hashtable is synchronized (thread safe)
Hashtable.IsSynchronized Property is used to get a value indicating whether access to the Hashtable is synchronized(thread-safe).
Syntax:
public virtual bool IsSynchronized { get; }
Return Value: This property return true if access to the Hashtable is synchronized (thread-safe), otherwise it returns false. The default is false.
Below programs illustrate the use of above-discussed property:
Example 1:
// C# code to check if Hashtable // Is Synchronized or not using System; using System.Collections; class GFG { // Driver code public static void Main() { // create and initialize Hashtable // using Add() method Hashtable has1 = new Hashtable(); has1.Add("1", "Welcome"); has1.Add("2", "to"); has1.Add("3", "geeks"); has1.Add("4", "for"); has1.Add("5", "geeks"); // Creates a synchronized // wrapper around the Hashtable. Hashtable smyTable = Hashtable.Synchronized(has1); // Displays the synchronization // status of both Hashtables. Console.WriteLine("has1 is {0}.", has1.IsSynchronized ? "Synchronized" : "Not Synchronized"); Console.WriteLine("smyTable is {0}.", smyTable.IsSynchronized ? "Synchronized" : "Not Synchronized"); } } |
has1 is Not Synchronized. smyTable is Synchronized.
Example 2:
// C# code to check if Hashtable // Is Synchronized or not using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating a Hashtable Hashtable myTable = new Hashtable(); // Adding elements in Hashtable myTable.Add("G", "Geeks"); myTable.Add("C", "C#"); myTable.Add("D", "Data Structures"); myTable.Add("Q", "Quiz"); // Checking if Hashtable is // synchronized (thread safe) Console.WriteLine(myTable.IsSynchronized); } } |
False
Note: A Hashtable can support one writer and multiple readers concurrently. To support multiple writers, all operations must be done through the wrapper returned by the Synchronized method.
Reference:
Recommended Posts:
- C# | Creating a synchronized (thread-safe) wrapper for the Hashtable
- C# | Check if ArrayList is Synchronized (thread safe)
- C# | Check if StringDictionary is synchronized (thread safe)
- C# | Check if the BitArray is synchronized (thread safe)
- C# | Check if ListDictionary is synchronized (thread safe)
- C# | Check if StringCollection is synchronized (thread safe)
- C# | Check if HybridDictionary is Synchronized (thread safe)
- C# | Check if an array is synchronized (thread safe) or not
- C# | Creating a synchronized (thread-safe) wrapper for the ArrayList
- C# | Creating a synchronized (thread-safe) wrapper for a SortedList object
- C# | Check if a Hashtable is equal to another Hashtable
- C# | Check if a thread belongs to managed thread pool or not
- How to check whether a thread is a background thread or not in C#
- C# | Check if a SortedList object is synchronized
- C# | Check if the Hashtable contains a specific Value
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.



