Curriculum
In C#, a Dictionary<TKey, TValue>
is a collection that stores key-value pairs, where each key is unique and associated with a single value. Here’s an example of using a Dictionary
to store and manipulate a collection of employee information:
using System; using System.Collections.Generic; class Program { static void Main(string[] args) { // Create a dictionary of employee information Dictionary<int, string> employees = new Dictionary<int, string>(); // Add some employee information to the dictionary employees.Add(1001, "Alice"); employees.Add(1002, "Bob"); employees.Add(1003, "Charlie"); // Access an employee's information by their employee ID string name = employees[1002]; Console.WriteLine("Employee 1002 is {0}", name); // Change an employee's information in the dictionary employees[1003] = "David"; // Remove an employee's information from the dictionary employees.Remove(1001); // Display the remaining employee information in the dictionary foreach (KeyValuePair<int, string> employee in employees) { Console.WriteLine("Employee {0} is {1}", employee.Key, employee.Value); } } }
In this example, we first create an empty Dictionary<int, string>
object called employees
. We then use the Add
method to add some employee information to the dictionary. We use the dictionary indexer (square brackets) to retrieve the name of the employee with ID 1002, and then change the name of the employee with ID 1003 using the indexer as well. We use the Remove
method to remove the employee with ID 1001 from the dictionary. Finally, we use a foreach
loop to display the remaining employee information in the dictionary.
Here are some other useful methods provided by the Dictionary
class:
Count
: Returns the number of key-value pairs in the dictionary.ContainsKey
: Returns true if the dictionary contains a specified key, otherwise false.ContainsValue
: Returns true if the dictionary contains a specified value, otherwise false.TryGetValue
: Tries to retrieve the value associated with a specified key, returning true if the key is found and false otherwise.Keys
: Returns a collection of all keys in the dictionary.Values
: Returns a collection of all values in the dictionary.Clear
: Removes all key-value pairs from the dictionary.Here’s an example that demonstrates some of these methods:
// Create a dictionary of string counts Dictionary<string, int> counts = new Dictionary<string, int>(); // Add some strings to the dictionary counts.Add("apple", 3); counts.Add("banana", 2); counts.Add("orange", 1); // Check if the dictionary contains a key bool hasApple = counts.ContainsKey("apple"); Console.WriteLine("Dictionary contains 'apple': {0}", hasApple); // Try to retrieve the value associated with a key int count; if (counts.TryGetValue("banana", out count)) { Console.WriteLine("Dictionary contains 'banana' with count {0}", count); } // Display the keys and values in the dictionary foreach (KeyValuePair<string, int> pair in counts) { Console.WriteLine("{0}: {1}", pair.Key, pair.Value); } // Clear the dictionary counts.Clear(); // Display the number of key-value pairs in the dictionary (which should be 0) Console.WriteLine("Number of key-value pairs in dictionary: {0}", counts.Count);
It’s important to note that Dictionary
provides many other useful properties and methods for working with key-value pairs, such as `GetEnumerator