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
CopyOnWriteArrayList version in C#
While Java has CopyOnWriteArrayList, C# does not have a direct equivalent. Instead, the SynchronizedCollection<T> class in C# provides thread-safe collection operations. Unlike Java's copy-on-write approach, SynchronizedCollection uses locking mechanisms to ensure thread safety.
Syntax
Following is the syntax for declaring a SynchronizedCollection −
public class SynchronizedCollection<T> : IList<T>, ICollection<T>,
IEnumerable<T>, IEnumerable, IList, ICollection
Where T is the type of objects in the collection.
Properties
The SynchronizedCollection class provides the following key properties −
| Property | Description |
|---|---|
| Count | Gets the number of elements in the thread-safe collection. |
| Item[Int32] | Gets or sets an element at the specified index in the collection. |
| Items | Gets the list of elements contained in the thread-safe collection. |
| SyncRoot | Gets the object used to synchronize access to the collection. |
Using SynchronizedCollection
Example
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
class Program {
static void Main() {
SynchronizedCollection<string> syncCollection = new SynchronizedCollection<string>();
// Add elements to the synchronized collection
syncCollection.Add("Apple");
syncCollection.Add("Banana");
syncCollection.Add("Orange");
Console.WriteLine("Collection Count: " + syncCollection.Count);
// Access elements safely
Console.WriteLine("First item: " + syncCollection[0]);
// Iterate through the collection
Console.WriteLine("\nAll items:");
foreach (string item in syncCollection) {
Console.WriteLine("- " + item);
}
}
}
The output of the above code is −
Collection Count: 3 First item: Apple All items: - Apple - Banana - Orange
Thread-Safe Operations Example
Example
using System;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;
class Program {
static SynchronizedCollection<int> numbers = new SynchronizedCollection<int>();
static void Main() {
// Start multiple threads to add numbers
Task.Run(() => AddNumbers(1, 100));
Task.Run(() => AddNumbers(101, 200));
Task.Run(() => AddNumbers(201, 300));
// Wait for operations to complete
Thread.Sleep(1000);
Console.WriteLine("Total numbers added: " + numbers.Count);
Console.WriteLine("First 5 numbers:");
for (int i = 0; i < Math.Min(5, numbers.Count); i++) {
Console.WriteLine(numbers[i]);
}
}
static void AddNumbers(int start, int end) {
for (int i = start; i <= end; i++) {
numbers.Add(i);
}
}
}
The output of the above code is −
Total numbers added: 300 First 5 numbers: 1 2 3 4 5
Comparison: SynchronizedCollection vs CopyOnWriteArrayList
| Feature | SynchronizedCollection (C#) | CopyOnWriteArrayList (Java) |
|---|---|---|
| Thread Safety | Uses synchronization locks | Creates copy on write operations |
| Read Performance | May block during writes | No blocking for reads |
| Write Performance | Better for frequent writes | Expensive due to copying |
| Memory Usage | Lower memory overhead | Higher due to copying |
Alternative: ConcurrentBag for High Performance
For scenarios requiring high-performance thread-safe collections, consider using ConcurrentBag<T> from System.Collections.Concurrent −
Example
using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
class Program {
static void Main() {
ConcurrentBag<string> bag = new ConcurrentBag<string>();
// Add items from multiple threads
Parallel.For(0, 10, i => {
bag.Add("Item " + i);
});
Console.WriteLine("Items in ConcurrentBag:");
foreach (string item in bag) {
Console.WriteLine(item);
}
Console.WriteLine("Total count: " + bag.Count);
}
}
The output of the above code is −
Items in ConcurrentBag: Item 9 Item 8 Item 7 Item 6 Item 5 Item 4 Item 3 Item 2 Item 1 Item 0 Total count: 10
Conclusion
While C# doesn't have CopyOnWriteArrayList, SynchronizedCollection<T> provides thread-safe collection operations using locking mechanisms. For high-performance scenarios, consider using collections from the System.Collections.Concurrent namespace like ConcurrentBag<T> or ConcurrentList<T>.
