Saturday, July 22, 2017

Difference among .NET Mutable, Readonly, Immutable, and Freezable Collections

Mutable 
Mutable is the most common collection type in the .NET world. These are collections such as List; that allow reading, as well as adding, removing and changing items.

Read-Only
Read-Only These are collections that can't be modified from the outside. However, this notion of collections doesn't guarantee that its contents will never change. For example, a dictionary's keys and values collections can't be updated directly, but adding to the dictionary indirectly updates the keys and values collections. You can create your own read only collection by inheriting from ReadOnlyCollection.

Immutable
Immutable These are collections that, once created, are guaranteed to never be changed and thread-safe. If a complicated data structure is fully immutable, it can always be safely passed to a background worker. You don't need to worry about someone modifying it at the same time. This collection type is not provided by the Microsoft .NET Framework base class library (BCL) today. You can download from NuGet by searching for Microsoft.Immutable.Collections.

Freezable
Freezable These collections behave like mutable collections until they're frozen. Then they behave like immutable collections. Although the BCL doesn't define these collections, you can find them in Windows Presentation Foundation.

No comments:

Thumbs Up to GitHub Copilot and JetBrains Resharper

Having used AI tool GitHub Copilot since 08/16/2023, I’ve realized that learning GitHub Copilot is like learning a new framework or library ...