C# Immutable List vs. ReadOnlyCollection
In C#, ImmutableList<T> and ReadOnlyCollection<T> both provide ways to prevent modification of data collections. However, they serve different purposes and have unique characteristics. Understanding the differences between these two can help you choose the right collection for your application. This article explores their distinctions, use cases, and provides practical examples.
Key Differences
Mutability
- ImmutableList: Completely immutable, meaning it cannot be changed after creation. Modifications return a new list while keeping the original list unchanged.
- ReadOnlyCollection: Read-only wrapper around an existing collection. The underlying collection can still be modified if a reference to it exists.
Thread Safety
- ImmutableList: Inherently thread-safe due to its immutability.
- ReadOnlyCollection: Not inherently thread-safe if the underlying collection is modified.
Modification Methods
- ImmutableList: Provides Add, Remove, Insert, and other methods that return a new collection.
- ReadOnlyCollection: Does not provide modification methods.
Example: Using ImmutableList<T>
The following example demonstrates how to create and manipulate an immutable list in C#:
using System;
using System.Collections.Immutable;
public class ImmutableListExample
{
public static void Main()
{
// Create an immutable list
ImmutableList<string> cities = ImmutableList.Create("New York", "London", "Tokyo");
// Add a new item, creating a new list
ImmutableList<string> updatedCities = cities.Add("Paris");
Console.WriteLine("Original List:");
foreach (var city in cities)
{
Console.WriteLine(city);
}
Console.WriteLine("\nUpdated List:");
foreach (var city in updatedCities)
{
Console.WriteLine(city);
}
}
}
Example: Using ReadOnlyCollection<T>
In this example, a List<T> is wrapped in a ReadOnlyCollection<T>. Note that changes made to the original list reflect in the read-only collection:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
public class ReadOnlyCollectionExample
{
public static void Main()
{
// Create a List and a ReadOnlyCollection wrapping it
List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" };
ReadOnlyCollection<string> readOnlyFruits = new ReadOnlyCollection<string>(fruits);
Console.WriteLine("ReadOnlyCollection before modification:");
foreach (var fruit in readOnlyFruits)
{
Console.WriteLine(fruit);
}
// Modify the original list
fruits.Add("Date");
Console.WriteLine("\nReadOnlyCollection after modifying original list:");
foreach (var fruit in readOnlyFruits)
{
Console.WriteLine(fruit);
}
}
}
Choosing Between ImmutableList and ReadOnlyCollection
- Use ImmutableList: When you need a truly immutable collection that guarantees no modifications.
- Use ReadOnlyCollection: When you only need to expose read-only data, but modifications may still occur internally.
Conclusion
Both ImmutableList<T> and ReadOnlyCollection<T> provide ways to limit modifications to collections in C#. By understanding their differences, you can choose the most appropriate collection for your application's needs.