fork download
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace DictionaryExample
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. // Create a dictionary in which both keys and values are strings.
  11. Dictionary<string, string> surfDict = new Dictionary<string, string>();
  12.  
  13. // Add some items to the dictionary: each has a key and a value.
  14. surfDict.Add ("Mavericks", "Big waves, cold water, and lots of Great White Sharks!");
  15. surfDict.Add ("Teahupoo", "Heaviest wave in the world, in Tahiti, so it is warm but very shallow.");
  16. surfDict.Add ("Jaws", "Warm water break that reaches 60 feet high, north shore of Maui, HI");
  17. surfDict.Add ("Nazare", "Worlds biggest surfed wave, up to 100+ feet, in Portugal");
  18. surfDict.Add ("Cocoa Beach", "Small, crappy wave in Cocoa Beach FL, home of Kelly Slater and lots of small sharks.");
  19.  
  20.  
  21. // See if the dictionary contains a particular key.
  22. Console.WriteLine ("Use the ContainsKey method to see if a surf breaks exists in your surf break Dictionary:");
  23. Console.WriteLine ("Contains key Mavericks " + surfDict.ContainsKey ("Mavericks"));
  24. Console.WriteLine ("Contains key Teahupoo " + surfDict.ContainsKey ("Teahupoo"));
  25. Console.WriteLine ("Contains key Nahant Beach " + surfDict.ContainsKey ("Nahant Beach"));
  26.  
  27. // Iterate the dictionary's contents with foreach.
  28. // Note that you're iterating pairs of keys and values,
  29. // using the KeyValuePair<t,u> type.
  30. Console.WriteLine ("\nContents of the dictionary:");
  31. foreach (KeyValuePair<string, string> pair in surfDict)
  32. {
  33. // Because the key is a string, you can use string methods
  34. Console.WriteLine ("Key: " + pair.Key.PadRight(8) + " - Value: " + pair.Value);
  35. }
  36.  
  37. // List the keys, remember they are in no particular order.
  38. Console.WriteLine ("\nJust the keys:");
  39.  
  40. // Dictionary<TKey, TValue>.KeyCollection is a collection of just the keys,
  41. // in this case strings. So here's how to retrieve the keys:
  42. Dictionary<string, string>.KeyCollection keys = surfDict.Keys;
  43. foreach(string key in keys)
  44. {
  45. Console.WriteLine ("Key: " + key);
  46. }
  47.  
  48. // List the values, which are in same order as key collection above.
  49. Console.WriteLine ("\nJust the values:");
  50. Dictionary<string, string>.ValueCollection values = surfDict.Values;
  51. foreach (string value in values)
  52. {
  53. Console.WriteLine ("Value: " + value);
  54. }
  55.  
  56. Console.Write ("\nNumber of items in the dictionary: " + surfDict.Count);
  57.  
  58. }
  59. }
  60. }
Success #stdin #stdout 0.03s 26380KB
stdin
Standard input is empty
stdout
Use the ContainsKey method to see if a surf breaks exists in your surf break Dictionary:
Contains key Mavericks       True
Contains key Teahupoo     True
Contains key Nahant Beach False

Contents of the dictionary:
Key: Mavericks -  Value: Big waves, cold water, and lots of Great White Sharks!
Key: Teahupoo -  Value: Heaviest wave in the world, in Tahiti, so it is warm but very shallow.
Key: Jaws     -  Value: Warm water break that reaches 60 feet high, north shore of Maui, HI
Key: Nazare   -  Value: Worlds biggest surfed wave, up to 100+ feet, in Portugal
Key: Cocoa Beach -  Value: Small, crappy wave in Cocoa Beach FL, home of Kelly Slater and lots of small sharks.

Just the keys:
Key: Mavericks
Key: Teahupoo
Key: Jaws
Key: Nazare
Key: Cocoa Beach

Just the values:
Value: Big waves, cold water, and lots of Great White Sharks!
Value: Heaviest wave in the world, in Tahiti, so it is warm but very shallow.
Value: Warm water break that reaches 60 feet high, north shore of Maui, HI
Value: Worlds biggest surfed wave, up to 100+ feet, in Portugal
Value: Small, crappy wave in Cocoa Beach FL, home of Kelly Slater and lots of small sharks.

Number of items in the dictionary: 5