C# | Check if HybridDictionary is Synchronized (thread safe)
Last Updated :
01 Feb, 2019
HybridDictionary.IsSynchronized property is used to get a value that indicates whether the HybridDictionary is synchronized (thread safe) or not.
Syntax:
public bool IsSynchronized { get; }
Return Value: This property always returns false.
Below programs illustrate the use of HybridDictionary.IsSynchronized property:
Example 1:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
HybridDictionary myDict = new HybridDictionary();
myDict.Add( "I" , "first" );
myDict.Add( "II" , "second" );
myDict.Add( "III" , "third" );
myDict.Add( "IV" , "fourth" );
myDict.Add( "V" , "fifth" );
Console.WriteLine(myDict.IsSynchronized);
}
}
|
Output:
False
Example 2:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
HybridDictionary myDict = new HybridDictionary();
myDict.Add( "key1" , "value1" );
myDict.Add( "key2" , "value2" );
myDict.Add( "key3" , "value3" );
myDict.Add( "key4" , "value4" );
myDict.Add( "key5" , "value5" );
Console.WriteLine(myDict.IsSynchronized);
}
}
|
Output:
False
Note: Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
Reference:
Share your thoughts in the comments
Please Login to comment...