Array.IsSynchronized Property is used to get a value which indicates whether access to the Array is synchronized (thread-safe) or not.
Syntax:
public bool IsSynchronized { get; }
Property Value: This property always returns false for all arrays.
Below programs illustrate the use of above-discussed property:
Example 1:
using System;
namespace geeksforgeeks {
class GFG {
public static void Main()
{
int [, ] arr = new int [4, 2] {{1, 2 }, {3, 4},
{5, 6 }, {7, 8}};
Console.WriteLine( "Result: " + arr.IsSynchronized);
}
}
}
|
Example 2:
using System;
namespace geeksforgeeks {
class GFG {
public static void Main()
{
string [] topic;
topic = new string [] { null };
Console.WriteLine( "Result: " + topic.IsSynchronized);
}
}
}
|
Note:
- Array implements the IsSynchronized property because it is needed by the System.Collections.ICollection interface.
- Classes which uses the arrays can also implement their own synchronization using the SyncRoot property.
- Enumerating through a collection is 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.
- Retrieving the value of this property is an O(1) operation.
Reference: