fork download
  1. // Naomi Jones
  2. // Survey, Summer 2025
  3. // July 6, 2025
  4. // Assignment 6 - 2 C# Dictionary
  5.  
  6. using System;
  7. using System.Collections.Generic;
  8.  
  9. namespace DictionaryOpera
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. // Creates a dictionary in which both keys and values are strings.
  16. Dictionary<string, string> dict = new Dictionary<string, string>();
  17.  
  18. // Add items to the dictionary. Each has a key and a value.
  19. dict.Add("Don Giovanni", "Serial seducer ends up in hell.");
  20. dict.Add("Un Ballo in Maschera", "Everyone ends up at a masked ball where someone gets stabbed.");
  21. dict.Add("Aida", "Unhappy love triangle.");
  22. dict.Add("Der fliegende Holländer", "A ghostly captain gets a shot at redemption every seven years.");
  23. dict.Add("Fidelio", "Cosplay jail break.");
  24. dict.Add("Turandot", "Princess gives death-or-marriage riddles, a bold prince takes a chance, and everyone stays up all night trying to figure out who he is.");
  25.  
  26. // Decided to remove Fidelio and add Gotterdammerung.
  27. dict.Remove("Fidelio");
  28. dict.Add("Gotterdammerung", "The last chapter of 15 hours of opera.");
  29.  
  30. // Displays the contents of the dictionary.
  31. Console.WriteLine("A list of favorite operas: ");
  32. foreach (KeyValuePair<string, string> pair in dict)
  33. {
  34. // String methods to format the output since both are strings.
  35. Console.WriteLine("Key: " + pair.Key.PadRight(25) + " Value: " + pair.Value);
  36. }
  37.  
  38. // Method to check if particular keys are contained in the dictionary.
  39. Console.WriteLine ("\nUsing the ContainsKey method to see if an opera exists in the dictionary:");
  40. Console.WriteLine ("Contains key Carmen " + dict.ContainsKey ("Carmen"));
  41. Console.WriteLine ("Contains key La Boheme " + dict.ContainsKey ("La Boheme"));
  42. Console.WriteLine ("Contains key Don Giovanni " + dict.ContainsKey ("Don Giovanni"));
  43.  
  44. // Retrieving just the keys.
  45. Console.WriteLine ("\nPrinting just the keys:");
  46. Dictionary<string, string>.KeyCollection keys = dict.Keys;
  47. foreach(string key in keys)
  48. {
  49. Console.WriteLine ("Key: " + key);
  50. }
  51.  
  52. } // main
  53.  
  54. } // Program class
  55.  
  56. } // DictionaryOpera namespace
  57.  
Success #stdin #stdout 0.07s 28976KB
stdin
Standard input is empty
stdout
A list of favorite operas: 
Key: Don Giovanni              Value: Serial seducer ends up in hell.
Key: Un Ballo in Maschera      Value: Everyone ends up at a masked ball where someone gets stabbed.
Key: Aida                      Value: Unhappy love triangle.
Key: Der fliegende Holländer   Value: A ghostly captain gets a shot at redemption every seven years.
Key: Gotterdammerung           Value: The last chapter of 15 hours of opera.
Key: Turandot                  Value: Princess gives death-or-marriage riddles, a bold prince takes a chance, and everyone stays up all night trying to figure out who he is.

Using the ContainsKey method to see if an opera exists in the dictionary:
Contains key Carmen       False
Contains key La Boheme     False
Contains key Don Giovanni True

Printing just the keys:
Key: Don Giovanni
Key: Un Ballo in Maschera
Key: Aida
Key: Der fliegende Holländer
Key: Gotterdammerung
Key: Turandot