Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if ArrayList is Synchronized (thread safe) in C#
The IsSynchronized property in C# is used to check if an ArrayList is synchronized (thread-safe). By default, ArrayList is not synchronized, meaning it's not safe for concurrent access by multiple threads. However, you can create a synchronized wrapper using the ArrayList.Synchronized() method.
Syntax
To check if an ArrayList is synchronized −
bool isSync = arrayList.IsSynchronized;
To create a synchronized ArrayList wrapper −
ArrayList syncList = ArrayList.Synchronized(originalList);
Return Value
The IsSynchronized property returns a bool value:
trueif the ArrayList is synchronized (thread-safe)falseif the ArrayList is not synchronized
Checking Default ArrayList Synchronization
By default, a regular ArrayList is not synchronized. Here's how to check this −
Example
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args) {
ArrayList list1 = new ArrayList();
list1.Add("A");
list1.Add("B");
list1.Add("C");
list1.Add("D");
list1.Add("E");
Console.WriteLine("Elements in ArrayList1...");
foreach (string res in list1) {
Console.WriteLine(res);
}
Console.WriteLine("Is ArrayList synchronized? = " + list1.IsSynchronized);
}
}
The output of the above code is −
Elements in ArrayList1... A B C D E Is ArrayList synchronized? = False
Creating a Synchronized ArrayList
You can create a thread-safe version of ArrayList using the ArrayList.Synchronized() method −
Example
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args) {
ArrayList list1 = new ArrayList();
list1.Add("One");
list1.Add("Two");
list1.Add("Three");
list1.Add("Four");
list1.Add("Five");
Console.WriteLine("Elements in ArrayList...");
foreach (string res in list1) {
Console.WriteLine(res);
}
ArrayList syncList = ArrayList.Synchronized(list1);
Console.WriteLine("Original ArrayList synchronized? = " + list1.IsSynchronized);
Console.WriteLine("Synchronized wrapper synchronized? = " + syncList.IsSynchronized);
}
}
The output of the above code is −
Elements in ArrayList... One Two Three Four Five Original ArrayList synchronized? = False Synchronized wrapper synchronized? = True
Thread Safety Comparison
| ArrayList Type | IsSynchronized | Thread Safe | Performance |
|---|---|---|---|
| Regular ArrayList | False | No | Faster |
| Synchronized ArrayList | True | Yes | Slower (due to locking) |
Modern Alternatives
While ArrayList synchronization is available, modern C# applications typically use thread-safe collections from System.Collections.Concurrent namespace like ConcurrentBag<T> or List<T> with manual synchronization for better performance and type safety.
Conclusion
The IsSynchronized property helps determine if an ArrayList is thread-safe. Regular ArrayLists return false, while synchronized wrappers created using ArrayList.Synchronized() return true. Use synchronized ArrayLists only when multiple threads need concurrent access to the same collection.
