using System; using System.Collections.Immutable; class Program { static void Main(string[] args) { ImmutableDictionaryemployees = ImmutableDictionary.Create () .Add(1, "John") .Add(2, "Jane") .Add(3, "James"); IEnumerator > enumerator = employees.GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair employee = enumerator.Current; Console.WriteLine("Employee Id: {0}, Name: {1}", employee.Key, employee.Value); } } }
using System.Collections.Immutable; class Program { static void Main(string[] args) { ImmutableDictionaryThis example demonstrates how to iterate through an ImmutableDictionary using a foreach loop. It creates an ImmutableDictionary of fruit names and prices, and then iterates through the key/value pairs to display the information for each fruit. Package Library: System.Collections.Immutable.prices = ImmutableDictionary.Create () .Add("apple", 1) .Add("banana", 2) .Add("orange", 3); foreach (var item in prices) { string key = item.Key; int value = item.Value; System.Console.WriteLine("Item: {0}, Price: {1}", key, value); } } }