Immutable Collection addition in .NET 4.5

The BCL team has recently released preview of the Immutable collection for .NET 4.5 and Windows Store apps and can be acquired as NuGet package. When I delved in this topic I stumbled upon a really cool Channel 9 video discussing Immutable Collection for .NET.

The concept behind immutable Collection

Immutable suggests, data will not change. The requirement of immutability comes when you acquire a reference to a collection which you require not to change. You might consider making that collection readable but that would only mean that you alone can’t add into that collection.  What if the other end changes that collection? The reference you are holding will still reflect those changes. The situation is not only undesirable but may also lead to issues of thread safety if the source is adding items on a different thread.

A workaround to that is possible by means of frozen collection which also ensure that data will not change at-least after the snapshot is taken. However in the new Immutable Collection for .NET, BCL team has taken a step further in a forward looking manner. Here’s what Erik Meijer says about that step ahead,

“It implies if you were to change it (i.e. frozen collection), you have to copy the entire collection. These immutable collections that we offer both never change yet they efficiently change which sounds like a paradox but we do it by allocating new memory for the difference between the original collection and the new collection. So if you have a reference to a collection it really will never change but if you want a reference to a collection which is based on that with a few changes applied, you can do that and that takes very little CPU and very little memory.”