var builder = ImmutableDictionary.CreateBuilder(); builder.Add("apple", 1); builder.Add("orange", 2); builder.Add("banana", 3); var dictionary = builder.ToImmutable();
var builder = ImmutableDictionary.CreateBuilderIn this example, we create a new builder for a dictionary with key of type int and value of type string. We then add three key-value pairs to the builder using the indexer notation and convert the builder to an immutable dictionary using the ToImmutable method. The ImmutableDictionary class is part of the System.Collections.Immutable package that provides immutable collection classes for C#. The package can be installed via NuGet.(); builder[1] = "one"; builder[2] = "two"; builder[3] = "three"; var dictionary = builder.ToImmutable();