fork download
  1. // Sample Dictionary
  2. using System;
  3. using System.Collections.Generic;
  4.  
  5. namespace DictionaryExample
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. // Create a dictionary in which both keys and values are strings.
  12. Dictionary<string, string> dict = new Dictionary<string, string>();
  13.  
  14. // Add some items to the dictionary: each has a key and a value.
  15. dict.Add ("Porterhouse Steak", "14 oz. steak marinated in our house marinade and seasoned to perfection");
  16. dict.Add ("Roasted Duck", "Half duck roasted in a savory bourbon sauce and draped in fresh cherries");
  17. dict.Add ("Cobb Salad", "Rabbit food covered with eggs and meat");
  18. dict.Add ("French Onion Soup", "Giant crouton with brown stuff, cheese, and onions");
  19. dict.Add ("Grilled Cheese", "An american classic, white american cheese grilled between two slices of toast");
  20. dict.Add ("Lasagna", "How did this make it on the menu?");
  21. dict.Add ("Taco Supreme", "Yo quiero Taco Bell!");
  22.  
  23.  
  24. // See if the dictionary contains a particular key.
  25. Console.WriteLine ("Use the ContainsKey method to see if a movies exists in your dictionary:");
  26. Console.WriteLine ("Contains key Porterhouse Steak " + dict.ContainsKey ("Porterhouse Steak"));
  27. Console.WriteLine ("Contains key Roasted Duck " + dict.ContainsKey ("Roasted Duck"));
  28. Console.WriteLine ("Contains key Jelly Beans " + dict.ContainsKey ("Jelly Beans"));
  29.  
  30. // Iterate the dictionary's contents with foreach.
  31. // Note that you're iterating pairs of keys and values,
  32. // using the KeyValuePair<T,U> type.
  33. Console.WriteLine ("\nContents of the dictionary:");
  34. foreach (KeyValuePair<string, string> pair in dict)
  35. {
  36. // Because the key is a string, you can use string methods
  37. Console.WriteLine ("Key: " + pair.Key.PadRight(8) + " Value: " + pair.Value);
  38. }
  39.  
  40. // List the keys, remember they are in no particular order.
  41. Console.WriteLine ("\nJust the keys:");
  42.  
  43. // Dictionary<TKey, TValue>.KeyCollection is a collection of just the keys,
  44. // in this case strings. So here's how to retrieve the keys:
  45. Dictionary<string, string>.KeyCollection keys = dict.Keys;
  46. foreach(string key in keys)
  47. {
  48. Console.WriteLine ("Key: " + key);
  49. }
  50.  
  51. // List the values, which are in same order as key collection above.
  52. Console.WriteLine ("\nJust the values:");
  53. Dictionary<string, string>.ValueCollection values = dict.Values;
  54. foreach (string value in values)
  55. {
  56. Console.WriteLine ("Value: " + value);
  57. }
  58.  
  59. Console.Write ("\nNumber of items in the dictionary: " + dict.Count);
  60.  
  61. }
  62. }
  63. }
Success #stdin #stdout 0.03s 14924KB
stdin
Standard input is empty
stdout
Use the ContainsKey method to see if a movies exists in your dictionary:
Contains key Porterhouse Steak       True
Contains key Roasted Duck     True
Contains key Jelly Beans False

Contents of the dictionary:
Key: Porterhouse Steak  Value: 14 oz. steak marinated in our house marinade and seasoned to perfection
Key: Roasted Duck  Value: Half duck roasted in a savory bourbon sauce and draped in fresh cherries
Key: Cobb Salad  Value: Rabbit food covered with eggs and meat
Key: French Onion Soup  Value: Giant crouton with brown stuff, cheese, and onions
Key: Grilled Cheese  Value: An american classic, white american cheese grilled between two slices of toast
Key: Lasagna   Value: How did this make it on the menu?
Key: Taco Supreme  Value: Yo quiero Taco Bell!

Just the keys:
Key: Porterhouse Steak
Key: Roasted Duck
Key: Cobb Salad
Key: French Onion Soup
Key: Grilled Cheese
Key: Lasagna
Key: Taco Supreme

Just the values:
Value: 14 oz. steak marinated in our house marinade and seasoned to perfection
Value: Half duck roasted in a savory bourbon sauce and draped in fresh cherries
Value: Rabbit food covered with eggs and meat
Value: Giant crouton with brown stuff, cheese, and onions
Value: An american classic, white american cheese grilled between two slices of toast
Value: How did this make it on the menu?
Value: Yo quiero Taco Bell!

Number of items in the dictionary: 7