.NET8 Frozen collection

New Frozen collection (potentially) being introduced with .NET8

Home DailyDrop

Daily Knowledge Drop

.NET8 potentially introduces a new collection type called a Frozen collection - this post will explore the new collection, and see how to compares to existing collection implementations.

As .NET8 is still currently in alpha, the Frozen collection as well as any specific functionality it may offer could change before the final release.


Usage

To see how the Frozen collection operates, below it is used along with other common collection implementations:

// create a base list of 10 items
List<int> baseList = Enumerable
    .Range(1, 10).ToList();

ReadOnlyCollection<int> readonlyList = 
    baseList.AsReadOnly();
FrozenSet<int> frozenSet = baseList.ToFrozenSet();
ImmutableList<int> immutableList = 
    baseList.ToImmutableList();

// now add another item to the list
baseList.Add(11);

Console.WriteLine($"List count: {baseList.Count}");
Console.WriteLine($"ReadOnlyList count: {readonlyList.Count}");
Console.WriteLine($"FrozenSet count: {frozenSet.Count}");
Console.WriteLine($"ImmutableList count: {immutableList.Count}");

The output from the above:

List count: 11
ReadOnlyList count: 11
FrozenSet count: 10
ImmutableList count: 10

From this we can see, that when adding an item to the underlying list:

  • The ReadOnlyList count also increases, as it is a readonly view into the underlying list
  • The FrozenSet and ImmutableList count is not increased

So what's the difference between the FrozenSet and ImmutableList?


Set vs List

As per their name, the FrozenSet is a set, while the ImmutableList is a list - a set cannot contain duplicates and is unordered, unlike a list. Consider the following code, similar to the previous example, but now with duplicate items:


List<int> baseList = Enumerable.Range(1, 10)
    .ToList();
// add duplicate items to the base list
baseList.Add(1);
baseList.Add(2);
baseList.Add(3);

ReadOnlyCollection<int> readonlyList = 
    baseList.AsReadOnly();
FrozenSet<int> frozenSet = baseList.ToFrozenSet();
ImmutableList<int> immutableList = 
    baseList.ToImmutableList();

Console.WriteLine($"List count: {baseList.Count}");
Console.WriteLine($"ReadOnlyList count: {readonlyList.Count}");
Console.WriteLine($"FrozenSet count: {frozenSet.Count}");
Console.WriteLine($"ImmutableList count: {immutableList.Count}");

The output:

List count: 13
ReadOnlyList count: 13
FrozenSet count: 10
ImmutableList count: 13

The 3 duplicate items are automatically removed when converting the base list to a set.


Another collection type

So why the need for another specialized collection type - Steven Giesel benchmark's the performance of the FrozenSet against other collection types:

  • it is substantially quicker vs the other types when performing a lookup
  • it is however slower when creating the set vs creating the other types

Notes

A new specialized collection type, which might not see every day use by the majority, but which, with the right use case, can improve the performance of the code.


References

Frozen collections in .NET 8

Daily Drop 223: 15-12-2022

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net .net8 collection frozen