use frozen collections in C#

Learn extra at:

  • FrozenDictionary<TKey,TValue>: A FrozenDictionary represents a read-only dictionary that’s optimized for quick searches. You need to use this assortment when the gathering must be created as soon as and skim continuously.
  • FrozenSet<T>: A FrozenSet represents a read-only immutable set optimized for quick searches and enumeration. Like a FrozenDictionary, you can’t alter this assortment after creation.

As a result of each FrozenSet and FrozenDictionary are read-only collections, you can’t add, change, or take away objects in these collections.

Create frozen collections in C#

The next code snippet reveals how one can create a FrozenSet from a HashSet occasion, populate it with pattern information, after which seek for an merchandise inside it. Recall {that a} HashSet is an unordered assortment of distinctive components that helps set operations (union, intersection, and so on.) and makes use of a hash desk for storage.


var hashSet = new HashSet { "A", "B", "C", "D", "E" };
var frozenSet = hashSet.ToFrozenSet();
bool isFound = frozenSet.TryGetValue("A", out _);

Within the previous code snippet, the ToFrozenSet() methodology is used to transform a HashSet occasion to a FrozenSet occasion. The tactic TryGetValue() will return true on this instance as a result of the info looked for is current within the assortment.

The next code reveals how one can create a FrozenDictionary, retailer information in it, and seek for a selected key within the assortment.


var dictionary = new Dictionary
{
    { 1, "A" },{ 2, "B" },{ 3, "C" },{ 4, "D" },{ 5, "E" }
};
var frozenDictionary = dictionary.ToFrozenDictionary();
bool isFound = dictionary.TryGetValue(7, out _);

When the above code is executed, the TryGetValue methodology will return false as a result of the important thing 7 shouldn’t be out there within the assortment.

Benchmarking efficiency of frozen collections in .NET Core

Allow us to now benchmark the efficiency of a frozen assortment, particularly a FrozenSet, in opposition to different assortment varieties utilizing the BenchmarkDotNet library. For comparability, we’ll use a Listing, a HashSet, and an ImmutableHashSet.

Recall {that a} Listing is a mutable, strongly-typed, dynamically-sized, ordered assortment of components (even duplicates), and {that a} HashSet is a mutable, array-based assortment of distinctive components that’s optimized for quick look-ups. Observe that the time complexity for looking an merchandise in a HashSet is O(1), in comparison with a time complexity of O(n) for a Listing (the place n is the variety of components within the assortment). Clearly, a HashSet is helpful in instances the place fast entry is important. On the draw back, a HashSet consumes extra reminiscence than a Listing and can’t embody duplicate components. It’s best to use a Listing if you wish to retailer an ordered assortment of things (probably duplicates) and the place useful resource consumption is a constraint.

Turn leads into sales with free email marketing tools (en)

Leave a reply

Please enter your comment!
Please enter your name here