In C#, frozen, read-only, and immutable collections

 In C#, frozen, read-only, and immutable collections provide different levels of immutability and thread safety. Here's a comparison of these concepts:

Frozen Collections

Frozen collections are part of the System.Collections.Frozen namespace, introduced in .NET 7. They are designed for scenarios where a collection is built once and then used multiple times without modification. They offer optimized performance for lookups.

  1. Immutability:

    • Once frozen, the collection cannot be modified.
  2. Performance:

    • Optimized for fast lookup operations.
    • Can be more efficient than other collection types for read-heavy scenarios.
  3. Usage:

    • Best used when you have a collection that is built once and then accessed frequently.
  4. Example: ```csharp using System.Collections.Frozen;

var list = new List { 1, 2, 3 }; var frozenList = list.ToFrozenList();

Console.WriteLine(frozenList.Contains(2)); // True


### ReadOnly Collections
Read-only collections wrap existing collections to prevent modification. They are part of the `System.Collections.ObjectModel` namespace.

1. **Immutability**:
   - The read-only collection itself cannot be modified, but the underlying collection it wraps can still be changed if accessed directly.

2. **Performance**:
   - Performance is similar to the underlying collection since it's just a wrapper.

3. **Usage**:
   - Used to expose collections in a way that prevents modification, often for APIs.

4. **Example**:
```csharp
using System.Collections.ObjectModel;

var list = new List<int> { 1, 2, 3 };
var readOnlyList = new ReadOnlyCollection<int>(list);

Console.WriteLine(readOnlyList[1]); // 2

// readOnlyList[1] = 4; // This will not compile
// list[1] = 4; // This will modify the underlying collection

Immutable Collections

Immutable collections are part of the System.Collections.Immutable namespace. Once created, they cannot be changed. Any modification operations return a new collection with the modifications.

  1. Immutability:

    • Truly immutable. Once created, they cannot be changed.
  2. Performance:

    • May have overhead due to creation of new collections for modifications.
    • Thread-safe without requiring additional synchronization.
  3. Usage:

    • Ideal for multi-threaded scenarios and functional programming patterns.
  4. Example: ```csharp using System.Collections.Immutable;

var list = ImmutableList.Create(1, 2, 3); var newList = list.Add(4);

Console.WriteLine(list.Count); // 3 Console.WriteLine(newList.Count); // 4 ```

Summary

  • Frozen Collections: Optimized for performance with a focus on fast lookups after the collection is built. Ideal for read-heavy scenarios.
  • Read-Only Collections: Provide a read-only view of a modifiable collection. The underlying collection can still be modified directly.
  • Immutable Collections: Fully immutable and thread-safe. Modifications return new collections, making them suitable for concurrent scenarios and functional programming.

In choosing between these, consider whether you need true immutability (Immutable Collections), a read-only view (Read-Only Collections), or performance-optimized lookups (Frozen Collections).

Vikash Chauhan

C# & .NET experienced Software Engineer with a demonstrated history of working in the computer software industry.

Post a Comment

Previous Post Next Post

Contact Form