System.Collections.Immutable

 

It can be challenging to keep mutable state under control when multiple threads are involved. A common approach is to make use of immutable state that can be passed freely between different threads. Immutable collections are different from read-only collections in the sense that unlike read-only collections, they cannot be changed by the provider or consumer of the collection. For example, if you are enumerating a read-only collection, there is a possibility that the collection can be changed on another thread, causing data corruption. This scenario can’t occur if you are using an immutable collection.

NamespaceDescription
System.Collections.Immutable

The System.Collections.Immutable namespace contains interfaces and classes that define immutable collections. These classes are supported starting with .NET Framework 4.5. Use them to build apps that target the desktop, Windows Store, Portable Class Library and Windows Phone 8.

System.Linq

The System.Linq namespace provides classes and interfaces that support queries that use Language-Integrated Query (LINQ).

With immutable collections, you can:

  • Share a collection in a way that its consumer can be assured that the collection will never change.

  • Provide implicit thread safety in multi-threaded applications (no locks required to access collections).

  • Follow functional programming practices.

  • Modify a collection during enumeration, while ensuring that the original collection does not change.

The immutable collection classes are supported starting with the .NET Framework 4.5. Use them to build apps that target the desktop, the Windows Store, Portable Class Library, and Windows Phone 8. The immutable classes are not part of the core class library distributed with the .NET Framework; they are available via NuGet.

To install the immutable collections

  1. Open your project in Visual Studio and choose Manage NuGet Packages from the Project menu.

  2. Optionally, select the Include prerelease checkbox . This option will give you access to new prerelease versions of the immutable classes, as they become available.

  3. Use the Search box to locate the System.Collections.Immutable package.

  4. In the left pane, select the System.Collections.Immutable package. In the right pane, select the desired version and then choose Install.

The installation steps described above are for Visual Studio 2015. For other versions of Visual Studio, the steps might be slightly different because of differences in the user interface (UI).

Return to top
Show: