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
What does Array.IsSynchronized property of array class do in C#?
The Array.IsSynchronized property in C# gets a boolean value indicating whether access to the Array is synchronized (thread-safe). This property is part of the ICollection interface implementation and helps determine if an array can be safely accessed by multiple threads simultaneously.
For standard C# arrays, this property always returns false, meaning that arrays are not inherently thread-safe. When multiple threads need to access the same array, you must implement your own synchronization using the SyncRoot property or other synchronization mechanisms.
Syntax
Following is the syntax for the Array.IsSynchronized property −
public bool IsSynchronized { get; }
Return Value
The property returns −
trueif access to the Array is synchronized (thread-safe)falseif access to the Array is not synchronized
For standard arrays in C#, this property always returns false.
Using IsSynchronized to Check Thread Safety
Example
using System;
class Program {
static void Main() {
int[] numbers = { 10, 20, 30, 40, 50 };
string[] names = { "Alice", "Bob", "Charlie" };
Console.WriteLine("Integer array IsSynchronized: " + numbers.IsSynchronized);
Console.WriteLine("String array IsSynchronized: " + names.IsSynchronized);
// Check if synchronization is needed
if (!numbers.IsSynchronized) {
Console.WriteLine("Array is not thread-safe. Use SyncRoot for synchronization.");
}
}
}
The output of the above code is −
Integer array IsSynchronized: False String array IsSynchronized: False Array is not thread-safe. Use SyncRoot for synchronization.
Using SyncRoot for Thread Safety
Since arrays are not synchronized by default, use the SyncRoot property with lock statements for thread-safe operations −
Example
using System;
class Program {
static void Main() {
Array arr = new int[] { 2, 1, 9, 4, 8, 6, 8 };
Console.WriteLine("Is array synchronized? " + arr.IsSynchronized);
Console.WriteLine("Using SyncRoot for thread-safe access:");
lock(arr.SyncRoot) {
foreach (Object val in arr) {
Console.WriteLine(val);
}
}
}
}
The output of the above code is −
Is array synchronized? False Using SyncRoot for thread-safe access: 2 1 9 4 8 6 8
Practical Thread Safety Example
Example
using System;
using System.Threading.Tasks;
class Program {
static int[] sharedArray = { 1, 2, 3, 4, 5 };
static void Main() {
Console.WriteLine("Array IsSynchronized: " + sharedArray.IsSynchronized);
// Simulate concurrent access
Task.Run(() => ModifyArray("Thread 1"));
Task.Run(() => ModifyArray("Thread 2"));
System.Threading.Thread.Sleep(1000); // Wait for tasks
Console.WriteLine("Final array values:");
PrintArray();
}
static void ModifyArray(string threadName) {
if (!sharedArray.IsSynchronized) {
lock(sharedArray.SyncRoot) {
Console.WriteLine($"{threadName} accessing array safely");
for (int i = 0; i < sharedArray.Length; i++) {
sharedArray[i] *= 2;
}
}
}
}
static void PrintArray() {
lock(sharedArray.SyncRoot) {
foreach (int val in sharedArray) {
Console.Write(val + " ");
}
Console.WriteLine();
}
}
}
The output of the above code is −
Array IsSynchronized: False Thread 1 accessing array safely Thread 2 accessing array safely Final array values: 4 8 12 16 20
Conclusion
The Array.IsSynchronized property indicates whether an array is thread-safe, and it always returns false for standard C# arrays. When working with multiple threads, use the SyncRoot property with lock statements to ensure thread-safe access to array elements.
