Home » C# » ImmutableList and the namespace System.Collections.Immutable

ImmutableList and the namespace System.Collections.Immutable

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.


or with the package console with this line:

Install-Package Microsoft.Bcl.Immutable

using System.Collections.Immutable;

ImmutableList<int> array = ImmutableList.Create<int>(100, 101, 300);

List<int> myList = new List<int> { 100, 101, 300 };ImmutableList<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.


If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.