Home » New in .NET 8.0 [12]: Frozen object sets

New in .NET 8.0 [12]: Frozen object sets

by admin
New in .NET 8.0 [12]: Frozen object sets

.NET 8.0 provides two new FrozenSet object sets und FrozenDictionary in the new namespace System.Collections.Frozen. With these frozen quantities, you cannot change, add, or delete elements after creation. To do this, they deliver the contained elements faster than other available object sets in .NET.


Dr. Holger Schwichtenberg is the technical director of the expert network www.IT-Visions.de, which, with 53 renowned experts, supports numerous medium-sized and large companies through consulting and training as well as with software development. Through his appearances at numerous national and international conferences as well as more than 90 specialist books and more than 1,500 specialist articles, Holger Schwichtenberg is one of the best-known experts for .NET and web technologies in Germany.

You can create a frozen set using two new extension methods for the IEnumerable interface:

ToFrozenSet() returns the set as an instance of FrozenSet returnToFrozenDictionary() returns the set as an instance of FrozenDictionary back.

These methods create a “frozen” list or dictionary (with name-value pairs).

There are three differences between the ReadOnlyCollection, which has been available since the .NET Framework 2.0, which was released in 2005 and the FrozenSet now introduced in .NET 8.0:

Implementations of ReadOnlyCollection only represent the view of another set that is created with AsReadOnly(). On the other hand, FrozenSet is a standalone set.FrozenSet has no indexer: frozenSet× therefore does not work.FrozenSet cannot contain duplicates because sets do not allow duplicates

The following code shows the use of FrozenSet in comparison to other object sets:


/// Class System.Collections.Frozen.FrozenSet: /// A read-only simple list. /// Duplicates are automatically removed! ///
See also  On November 3, the Netflix subscription with advertising starts: 2 out of 3 Italians ready to change

public void FrozenSet()

CUI.H3(“FrozenSet aus Array”);
int[] Numbers = new int[] { 1, 2, 3 }; var f = Numbers.ToFrozenSet(); Console.WriteLine(f.GetType().FullName); CUI.H3(“FrozenSet from List”); Cunning list = new List { 42, 10, 50, 42 };
FrozenSet frozenSet = list.ToFrozenSet(); Console.WriteLine(frozenSet.GetType().FullName); Console.WriteLine(frozenSet.Count); // only 3 because there are no duplicates! #region ——————- // Change –> FrozenSet has no indexer, // no Add(), no Remove() //frozenSet[2] = 10; // Change not possible //frozenSet.Add(42); // Cannot add!! //frozenSet.Remove(42); // Cannot add!! #endregion CUI.H3(“ReadOnlyCollection from List”); // existed before .NET 8.0 🙂 –> // ReadOnlyCollection is just a view of another set, // which is created with AsReadOnly(). ReadOnlyCollection readonlyList = list.AsReadOnly();
//readonlyList[2] = 10; // Change not possible //readonlyList.Add(42); // Cannot add!! CUI.H3(“ImmutableList from List”); // existed before .NET 8.0 🙂 ImmutableList immutableList = list.ToImmutableList();
//immutableList[2] = 10; // Cannot change var newImmutableList = immutableList.Add(42); // Adding IS possible, but you get a new list! #region ——————- Changes in original list CUI.H3(“Change an element”); list[2] += 1; // change the third element of the original list Console.WriteLine(list[2]); // 51
Console.WriteLine(readonlyList[2]); // 51 Console.WriteLine(immutableList.ElementAt(2)); // is still 50! Console.WriteLine(frozenSet.ElementAt(2)); // is still 50! #endregion #region ——————- Add elements to origin list CUI.H3(“Add an element”); list.Add(77); // Add a fifth element Console.WriteLine($”List count: {list.Count}”); // == 5! // == 5 because: ReadOnlyList is a view of List!: Console.WriteLine($”ReadOnlyList count: {readonlyList.Count}”); // == 4 because previous state of the list: Console.WriteLine($”ImmutableList count: {immutableList.Count}”); // == 5 new list: Console.WriteLine($”new ImmutableList count: {newImmutableList.Count}”); // == 3 FrozenSET: previous state, no duplicates: Console.WriteLine($”FrozenSet count: {frozenSet.Count}”); #endregion }

The following code snippet shows how to use FrozenDictionary:

public void FrozenDic()
List list = new List { 42, 10, 50, 42 };

See also  GTA 6, the new American dream: the most anticipated video game of the moment has been revealed

CUI.H3(“FrozenDictionary aus List”);
FrozenDictionary frozenDic =
list.Distinct().ToFrozenDictionary(x => x,
x => “Zahl ” + x.ToString());
Console.WriteLine(frozenDic.Count); // 3
// frozenDic[0] = 2;
// frozenDic.TryAdd(0, “Zahl 0”); // “Not supported”



To home page

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy