static void Main(string[] args) { //Creates and initializes a new Diction aryBase. ShortStringDictionary mySSC = new ShortStringDictionary(); //Adds elements to the collection. mySSC.Add("One", "a"); //增加更多的项目到集合中 Console.WriteLine("Initial contents of the collection :"); PrintKeysAndValues(mySSC); }
public static void Main() { // Creates and initializes a new DictionaryBase. ShortStringDictionary mySSC = new ShortStringDictionary(); // Adds elements to the collection. mySSC.Add("One", "a"); mySSC.Add("Two", "ab"); mySSC.Add("Three", "abc"); mySSC.Add("Four", "abcd"); mySSC.Add("Five", "abcde"); // Display the contents of the collection using foreach. This is the preferred method. Console.WriteLine("Contents of the collection (using foreach):"); PrintKeysAndValues1(mySSC); // Display the contents of the collection using the enumerator. Console.WriteLine("Contents of the collection (using enumerator):"); PrintKeysAndValues2(mySSC); // Display the contents of the collection using the Keys property and the Item property. Console.WriteLine("Initial contents of the collection (using Keys and Item):"); PrintKeysAndValues3(mySSC); // Tries to add a value that is too long. try { mySSC.Add("Ten", "abcdefghij"); } catch (ArgumentException e) { Console.WriteLine(e.ToString()); } // Tries to add a key that is too long. try { mySSC.Add("Eleven", "ijk"); } catch (ArgumentException e) { Console.WriteLine(e.ToString()); } Console.WriteLine(); // Searches the collection with Contains. Console.WriteLine("Contains \"Three\": {0}", mySSC.Contains("Three")); Console.WriteLine("Contains \"Twelve\": {0}", mySSC.Contains("Twelve")); Console.WriteLine(); // Removes an element from the collection. mySSC.Remove("Two"); // Displays the contents of the collection. Console.WriteLine("After removing \"Two\":"); PrintKeysAndValues1(mySSC); }