System Namespace Array Class
Returns a value indicating whether access to the Array is synchronized ( thread-safe ).
[ VB ]
<Serializable>
Overridable Public ReadOnly Property IsSynchronized As Boolean _
Implements ICollection.IsSynchronized
[ C# ]
[Serializable]
public virtual bool IsSynchronized {get;}
[ C++ ]
[Serializable]
public: __property virtual bool get_IsSynchronized ( );
[JScript ]
public Serializable
function get IsSynchronized ( ) : Boolean;
This property is always false for all arrays.
ICollection.IsSynchronized
Array implements the IsSynchronized property because it is required by the System.Collections.ICollection interface.
.NET Framework classes based on Array provide their own synchronized version of the collection using the SyncRoot property.
Classes that use arrays can also implement their own synchronization using the SyncRoot property. The synchronizing code must perform operations on the SyncRoot of the collection, not directly on the collection. This ensures proper operation of collections that are derived from other objects. Specifically, it maintains proper synchronization with other threads that might be simultaneously modifying the collection. Note that some implementations of SyncRoot might return the Array itself.
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could 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.
The following code example shows how to lock a collection during the entire enumeration by using the SyncRoot:
[ C# ]
Array myCollection = new int [ ];
lock ( myCollection.SyncRoot ) {
foreach ( Object item in myCollection ) {
// Insert your code here.
}
}
[ VB ]
Dim myCollection As New Int [ ]
Dim item As Object
SyncLock myCollection.SyncRoot
For Each item In myCollection
' Insert your code here.
Next item
End SyncLock
Array Members SyncRoot System.Collections.ICollection