C# ArrayList IsSynchronized
Description
ArrayList IsSynchronized
gets a value indicating whether
access to the ArrayList is synchronized (thread safe).
Syntax
ArrayList.IsSynchronized
has the following syntax.
public virtual bool IsSynchronized { get; }
Example
The following code example shows how to lock the collection using the SyncRoot during the entire enumeration.
using System;//from w w w. j a v a2 s. co m
using System.Collections;
public class MainClass{
public static void Main(String[] argv){
ArrayList myCollection = new ArrayList();
lock(myCollection.SyncRoot)
{
foreach (object item in myCollection)
{
// Insert your code here.
}
}
}
}
The code above generates the following result.
Example 2
The following code example shows how to synchronize an ArrayList, determine if an ArrayList is synchronized and use a synchronized ArrayList.
using System;/*from w w w. ja va 2 s. com*/
using System.Collections;
public class SamplesArrayList {
public static void Main() {
ArrayList myAL = new ArrayList();
myAL.Add( "The" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
ArrayList mySyncdAL = ArrayList.Synchronized( myAL );
Console.WriteLine(myAL.IsSynchronized);
Console.WriteLine(mySyncdAL.IsSynchronized);
}
}
The code above generates the following result.