Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding items to an ImmutableList<T> inside an ImmutableDictionary<TKey, TValue>

I can't seem to figure out how add items to an ImmutableList inside of an ImmutableDictionary.

I have the following variable:

ImmutableDictionary<string, ImmutableList<string>> _attributes = ImmutableDictionary<string, ImmutableList<string>>.Empty;

To which I'm trying to add a value inside of the list:

string[] attribute = line.Split(':');
if (!_attributes.ContainsKey(attribute[0]))
    _attributes = _attributes.Add(attribute[0], ImmutableList<string>.Empty);
if (attribute.Length == 2)
    _attributes[attribute[0]] = _attributes[attribute[0]].Add(attribute[1]);

However, I get an error saying that the ImmutableList doesn't have a setter. How do I replace the list in the dictionary without having to rebuilt the entire Dictionary?

like image 421
jscarle Avatar asked Sep 03 '25 17:09

jscarle


2 Answers

The ImmutableCollections provide a bunch of different ways how you can construct them.
The general guidance is first populate them then make them immutable.

Create + AddRange

ImmutableDictionary<string, string> collection1 = ImmutableDictionary
    .Create<string, string>(StringComparer.InvariantCultureIgnoreCase)
    .AddRange(
        new[]
        {
            new KeyValuePair<string, string>("a", "a"),
            new KeyValuePair<string, string>("b", "b"),
        });

We have created an empty collection then created another one with some values.

Create + Builder

ImmutableDictionary<string, string>.Builder builder2 = ImmutableDictionary
    .Create<string, string>(StringComparer.InvariantCultureIgnoreCase)
    .ToBuilder();

builder2.AddRange(
    new[]
    {
        new KeyValuePair<string, string>("a", "a"),
        new KeyValuePair<string, string>("b", "b"),
    });

ImmutableDictionary<string, string> collection2 = builder2.ToImmutable();

We have created an empty collection then converted it to a builder.
We have populated it with values.
Finally we have constructed the immutable collection.

CreateBuilder

ImmutableDictionary<string, string>.Builder builder3 = ImmutableDictionary
    .CreateBuilder<string, string>(StringComparer.InvariantCultureIgnoreCase);

builder3
    .AddRange(
        new[]
        {
            new KeyValuePair<string, string>("a", "a"),
            new KeyValuePair<string, string>("b", "b"),
        });

ImmutableDictionary<string, string> collection3 = builder3.ToImmutable();

This is short form of the previous case (Create + ToBuilder)

CreateRange

ImmutableDictionary<string, string> collection4 = ImmutableDictionary
    .CreateRange(new[]
    {
        new KeyValuePair<string, string>("a", "a"),
        new KeyValuePair<string, string>("b", "b"),
    });

This is short form of the first case (Create + AddRange)

ToImmutableDictionary

ImmutableDictionary<string, string> collection5 = new Dictionary<string, string>
{
    { "a", "a" },
    { "b", "b" }
}.ToImmutableDictionary();

Last but not least here we have used a converter.

like image 161
Peter Csala Avatar answered Sep 05 '25 07:09

Peter Csala


The answer was under my nose. Just like I need to call Add and overwrite the original variable when adding items, I need to call SetItem on the Dictionary. Makes sense!

like image 38
jscarle Avatar answered Sep 05 '25 05:09

jscarle