Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Synchronized Method (IList)
Collapse the table of content
Expand the table of content

ArrayList.Synchronized Method (IList)

.NET Framework 1.1

Returns an IList wrapper that is synchronized (thread-safe).

[Visual Basic]
Overloads Public Shared Function Synchronized( _
   ByVal list As IList _
) As IList
[C#]
public static IList Synchronized(
 IList list
);
[C++]
public: static IList* Synchronized(
 IList* list
);
[JScript]
public static function Synchronized(
   list : IList
) : IList;

Parameters

list
The IList to synchronize.

Return Value

An IList wrapper that is synchronized (thread-safe).

Exceptions

Exception Type Condition
ArgumentNullException list is a null reference (Nothing in Visual Basic).

Remarks

To guarantee the thread safety of the ArrayList, all operations must be done through this wrapper.

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 the collection using the SyncRoot during the entire enumeration:

[C#] 
ArrayList myCollection = new ArrayList();
 lock( myCollection.SyncRoot ) {
 foreach ( Object item in myCollection ) {
 // Insert your code here.
 }
}
[Visual Basic] 
Dim myCollection As New ArrayList()
Dim item As Object
SyncLock myCollection.SyncRoot
 For Each item In myCollection
 ' Insert your code here.
 Next item
End SyncLock

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

ArrayList Class | ArrayList Members | System.Collections Namespace | ArrayList.Synchronized Overload List | SyncRoot | IsSynchronized

Show:
© 2015 Microsoft