Curriculum
In C#, a SortedDictionary<TKey, TValue>
is a collection that stores key-value pairs, where each key is unique and associated with a single value, and the keys are sorted in ascending order. Here’s an example of using a SortedDictionary
to store and manipulate a collection of student grades:
using System; using System.Collections.Generic; class Program { static void Main(string[] args) { // Create a sorted dictionary of student grades SortedDictionary<string, int> grades = new SortedDictionary<string, int>(); // Add some student grades to the dictionary grades.Add("Alice", 90); grades.Add("Bob", 80); grades.Add("Charlie", 70); // Access a student's grade by their name int grade = grades["Bob"]; Console.WriteLine("Bob's grade is {0}", grade); // Change a student's grade in the dictionary grades["Charlie"] = 75; // Remove a student's grade from the dictionary grades.Remove("Alice"); // Display the remaining student grades in the dictionary foreach (KeyValuePair<string, int> student in grades) { Console.WriteLine("{0}'s grade is {1}", student.Key, student.Value); } } }
In this example, we first create an empty SortedDictionary<string, int>
object called grades
. We then use the Add
method to add some student grades to the dictionary. We use the dictionary indexer (square brackets) to retrieve the grade of the student named “Bob”, and then change the grade of the student named “Charlie” using the indexer as well. We use the Remove
method to remove the grade of the student named “Alice” from the dictionary. Finally, we use a foreach
loop to display the remaining student grades in the dictionary, which are automatically sorted by name.
Here are some other useful methods provided by the SortedDictionary
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.In addition to these methods, SortedDictionary
also provides methods for working with the sorted order of the keys, such as First
, Last
, RemoveFirst
, and RemoveLast
.
Here’s an example that demonstrates some of these methods:
// Create a sorted dictionary of string counts SortedDictionary<string, int> counts = new SortedDictionary<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); } // Remove the first and last elements from the dictionary counts.RemoveFirst();
counts.RemoveLast();
Console.WriteLine(“Dictionary after removing first and last elements:”);
foreach (KeyValuePair<string, int> pair in counts)
{
Console.WriteLine(“{0}: {1}”, pair.Key, pair.Value);
}
// Clear the dictionary
counts.Clear();
Console.WriteLine(“Dictionary after clearing:”);
Console.WriteLine(“Count: {0}”, counts.Count);
In this example, we create a SortedDictionary<string, int>
object called counts
and add some string counts to it. We use the ContainsKey
method to check if the dictionary contains a key “apple”, and the TryGetValue
method to retrieve the value associated with the key “banana”. We use a foreach
loop to display all the keys and values in the dictionary in sorted order. We then use the RemoveFirst
and RemoveLast
methods to remove the first and last elements from the dictionary, respectively. Finally, we use the Clear
method to remove all elements from the dictionary.
Overall, SortedDictionary
is a useful collection for storing and manipulating key-value pairs in a sorted order. It provides a rich set of methods for working with the keys and values, and its automatic sorting feature makes it easy to work with the data in a predictable way.