This includes the Task Parallel Library (TPL) as well as the new async/await keywords features to reduce the friction when writing asynchronous code. However, it’s still 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. Unfortunately, implementing immutable data structures can be quite hard, especially when collections are involved. In this post, Andrew Arnott, a developer from the Visual Studio team, will talk about a set of collection types we developed to make that easier. –Immo
Today [GD: December 18th] we released a preview of the immutable collections for the .NET Framework 4.5 and Windows Store apps on NuGet, as planned.
If you’re not already familiar with immutability concepts and where it’s valuable in your application you may want to read Read-only, frozen, and immutable collections.
...Why should I use immutability?
There are many good reasons to use immutable collections. Here are a few:
Introducing the immutable collections
- Snapshot semantics, allowing you to share your collections in a way that the receiver can count on never changing.
- Implicit thread-safety in multi-threaded applications (no locks required to access collections).
- Any time you have a class member that accepts or returns a collection type and you want to include read-only semantics in the contract.
- Functional programming friendly.
- Allow modification of a collection during enumeration, while ensuring original collection does not change.
- They implement the same IReadOnly* interfaces that your code already deals with so migration is easy.
The NuGet package preview includes these types:
- ImmutableDictionary<K, V>
- ImmutableSortedDictionary<K, V>
Interfaces for each of these types are also defined to facilitate exchange of immutable collection types that may be implemented differently to optimize for very specific performance or memory requirements.
How do immutable collections perform relative to their well-known mutable BCL counterparts? These immutable collections have been heavily tuned for maximum performance and minimum GC pressure. They’re very fast, and in most cases they will be appropriate to use in your applications.
...How can I get started?
The NuGet package will be in a prerelease form for a brief time, giving you an opportunity to tell us what you think so we can make necessary changes. Add comments to this post, or contact the authors of our NuGet package.
I had missed this over the holiday period. And since the BCL teams are one of my favorites (alone with most of the teams in DevDiv... ;), anything coming them gets a free mention!
I just dig seeing their samples and previews as they often foreshadow what we might be seeing in the next .Net version. That and I'm just glad there IS someone on the BCL team... :P (See, .Net isn't dead! The BCL team is still coding their little fingers off!... )