// Create an empty ImmutableDictionary var immutableDictionary = ImmutableDictionary.Empty; // Add elements to the dictionary using Add method immutableDictionary = immutableDictionary.Add("apple", 1); immutableDictionary = immutableDictionary.Add("banana", 2); immutableDictionary = immutableDictionary.Add("orange", 3);
// Create an ImmutableDictionary with some elements var immutableDictionary = ImmutableDictionaryThis code creates an ImmutableDictionary with three elements using the Add method. In the second part, the Add method is used to try adding an existing key "apple" along with a new value. Since ImmutableDictionary is immutable, it creates a new dictionary with the same elements and returns the new dictionary without updating the old one. Package: System.Collections.Immutable.Empty .Add("apple", 1) .Add("banana", 2) .Add("orange", 3); // Try to add an existing key using Add method immutableDictionary = immutableDictionary.Add("apple", 4);