If you are given a List<T> or even a ReadOnlyList<T> what guarantee is there that the list will not change? The answer is there is no guarantee. The owner of that list can change the contents at any time. What if a class could give you that guarantee?  The Systems.Collections.Immutable namespace does just that. In that namespace, there are different collection types including list, queue, stack, dictionary and more!

When you are given an immutable collection you know items will not be added or removed. This is especially important in multi-threaded where two threads working with the same collection can cause an exception to be throw.

System.Collections.Immutable is an out of band (OOB) release and therefore must be installed via NuGet. An OOB feature is a part of the .NET framework but it ships separately. Want to know more about what an out of band release is? Read about it here.

Immutable list example:

For more information, see

https://msdn.microsoft.com/en-us/library/mt452182(v=vs.111).aspx

This blog post from Microsoft also provide good information

https://blogs.msdn.microsoft.com/bclteam/2012/12/18/preview-of-immutable-collections-released-on-nuget/

 

Leave a Reply

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