// create an ImmutableDictionary var immutableDictionary = ImmutableDictionary.Empty; // add key, value pairs to ImmutableDictionary immutableDictionary = immutableDictionary.Add("One", 1); immutableDictionary = immutableDictionary.Add("Two", 2); immutableDictionary = immutableDictionary.Add("Three", 3); // get value using key from ImmutableDictionary var value = immutableDictionary["Two"]; // print all key, value pairs from ImmutableDictionary foreach (var kvp in immutableDictionary) { Console.WriteLine($"{kvp.Key}: {kvp.Value}"); }
// create an ImmutableDictionary var builder = ImmutableDictionary.CreateBuilder(); builder["One"] = 1; builder["Two"] = 2; builder["Three"] = 3; var immutableDictionary = builder.ToImmutable(); // remove a key, value pair from ImmutableDictionary immutableDictionary = immutableDictionary.Remove("Two"); // check if ImmutableDictionary contains a key if (immutableDictionary.ContainsKey("One")) { Console.WriteLine("ImmutableDictionary contains key 'One'"); }