Dictionarynumbers = new Dictionary (); // Add key-value pairs to the dictionary numbers.Add("one", 1); numbers.Add("two", 2); numbers.Add("three", 3); // Access a value using a key int result = numbers["two"]; Console.WriteLine(result); // Output: 2
DictionaryIn both examples, the Dictionary class is used to store key-value pairs, and the values can be accessed using the key. The System.Collections package library is used to access the Dictionary class.animals = new Dictionary (); animals.Add(1, "lion"); animals.Add(2, "tiger"); animals.Add(3, "bear"); string result = animals[2]; Console.WriteLine(result); // Output: tiger