This documentation is archived and is not being maintained.

Array.IsSynchronized Property

Gets a value indicating whether access to the Array is synchronized (thread-safe).

[Visual Basic]
Public Overridable ReadOnly Property IsSynchronized As Boolean  _
   Implements ICollection.IsSynchronized
public virtual bool IsSynchronized {get;}
public: __property virtual bool get_IsSynchronized();
public function get IsSynchronized() : Boolean;

Property Value

This property is always false for all arrays.




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.

[Visual Basic, C#] The following code example shows how to lock a collection during the entire enumeration by using the SyncRoot:

Array myCollection = new int[];
 lock( myCollection.SyncRoot ) {
 foreach ( Object item in myCollection ) {
 // Insert your code here.
[Visual Basic] 
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


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Array Class | Array Members | System Namespace | SyncRoot | System.Collections.ICollection