Using Immutable Collections

In section Working With Collections, we explained the need to replace standard .NET collections by special advisable collections of the PostSharp.Patterns.Collections namespace. These collections come with a significant inconvenient: they have a significant performance and memory overhead. In many situations, collections can be replaced by immutable collections. Immutable collections are collections whose content never changes after instantiation. Adequate use of immutable collections can significantly improve application performance and simplify API design compared to mutable collections, whether standard or advisable.

Immutable collections are implemented in the System.Collections.Immutable namespace, contained in the System.Collections.Immutable NuGet package.

The Aggregatable pattern and threading models support immutable collections. When you assign an immutable collection to a child field of a parent object, items of the collection become children of the parent object. Immutable collections behave similarly than other types, so you still have to use the ChildAttribute and ReferenceAttribute custom attributes as usually.

See Also