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
How to get Synchronize access to the StringCollection in C#
The StringCollection class in C# is not thread-safe by default. To achieve synchronized access in multi-threaded environments, you can use the SyncRoot property combined with a lock statement to ensure thread-safe operations.
The SyncRoot property returns an object that can be used to synchronize access to the collection, preventing multiple threads from modifying or accessing the collection simultaneously.
Syntax
Following is the syntax for synchronized access to StringCollection −
lock(stringCollection.SyncRoot) {
// thread-safe operations on stringCollection
}
Why Synchronization is Needed
In multi-threaded applications, multiple threads may try to access or modify a collection simultaneously, which can lead to data corruption or unexpected behavior. The lock statement ensures that only one thread can access the collection at a time within the locked block.
Using SyncRoot for Thread-Safe Access
Example 1
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection stringCol = new StringCollection();
String[] arr = new String[] { "100", "200", "300", "400", "500" };
Console.WriteLine("Array elements...");
foreach (string res in arr) {
Console.WriteLine(res);
}
stringCol.AddRange(arr);
Console.WriteLine("Total number of elements = " + stringCol.Count);
stringCol.RemoveAt(3);
Console.WriteLine("Total number of elements now = " + stringCol.Count);
Console.WriteLine("Synchronize access...");
lock(stringCol.SyncRoot) {
foreach(object ob in stringCol) {
Console.WriteLine(ob);
}
}
}
}
The output of the above code is −
Array elements... 100 200 300 400 500 Total number of elements = 5 Total number of elements now = 4 Synchronize access... 100 200 300 500
Example 2
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection strCol = new StringCollection();
strCol.Add("Katie");
strCol.Add("Philips");
strCol.Add("Jacob");
strCol.Add("Carl");
strCol.Add("Gary");
Console.WriteLine("Synchronize access...");
lock(strCol.SyncRoot) {
foreach(object ob in strCol) {
Console.WriteLine(ob);
}
}
}
}
The output of the above code is −
Synchronize access... Katie Philips Jacob Carl Gary
Thread-Safe Operations Example
Example 3
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringCollection collection = new StringCollection();
collection.Add("Item1");
collection.Add("Item2");
collection.Add("Item3");
Console.WriteLine("Performing synchronized operations...");
// Thread-safe read operation
lock(collection.SyncRoot) {
Console.WriteLine("Count: " + collection.Count);
if (collection.Contains("Item2")) {
Console.WriteLine("Item2 found in collection");
}
}
// Thread-safe write operation
lock(collection.SyncRoot) {
collection.Add("Item4");
collection.Remove("Item1");
Console.WriteLine("After modifications, Count: " + collection.Count);
}
// Thread-safe iteration
lock(collection.SyncRoot) {
Console.WriteLine("Final collection:");
foreach(string item in collection) {
Console.WriteLine(item);
}
}
}
}
The output of the above code is −
Performing synchronized operations... Count: 3 Item2 found in collection After modifications, Count: 3 Final collection: Item2 Item3 Item4
Conclusion
Using the SyncRoot property with lock statements ensures thread-safe access to StringCollection in multi-threaded environments. This approach prevents data corruption and race conditions by allowing only one thread to access the collection at a time within the synchronized block.
