ImmutableList and the namespace System.Collections.Immutable<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

ImmutableList and the namespace System.Collections.Immutable

Posted on: October 24, 2013

An immutable list has the propriety of being not modifiable after its creation. String is immutable because each time you change its value a new instance is created. If you want to have the same behavior with a list, you need to use an immutable list.

The creation of an ImmutableArray can be created from a method or from another list. But first, you need to use NuGet to download the package. You also need to use .Net Framework 4.5.

MicrosoftImmutableCollection 400x335

or with the package console with this line:

1Install-Package Microsoft.Bcl.Immutable
1using System.Collections.Immutable;
3ImmutableList<int> array = ImmutableList.Create<int>(100, 101, 300);
5List<int> myList = new List<int> { 100, 101, 300 };
6ImmutableList<int> array2 = ImmutableList.ToImmutableList<int>(myList);

As you can see, you need to use the namespace System.Collections.Immutable. The namespace contains more than only ImmutableList, but also contains an ImmutableDictionary, ImmutableQueue, ImmutableSet and ImmutableStack.