fork(1) 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 ("Aprilia Rsv4", "An Aprilia made sport bike");
  16. dict.Add ("Yamaha R1M", "Yamahas super sport bike");
  17. dict.Add ("ZX10RR", "Kawasakis Sport bike with a more performance built motor");
  18. dict.Add ("Ducati V4s", "Ducatis more track driven V4 model");
  19. dict.Add ("Honda CBR Fireblade", "Hondas new sport bike");
  20. dict.Add ("BMW S1000RR", "BMWs made sport bike");
  21. dict.Add ("GSXR 1000", "Suzukis liter sport bike");
  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 Aprilia Rsv4 " + dict.ContainsKey ("Rudy"));
  27. Console.WriteLine ("Contains key Yamaha R1M " + dict.ContainsKey ("Patton"));
  28. Console.WriteLine ("Contains key Ducati V4s " + dict.ContainsKey ("State Fair"));
  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.  
  60. Console.WriteLine("\nContents of the dictionary sorted by keys:");
  61. foreach (var pair in new SortedDictionary<string, string>(dict))
  62. {
  63. Console.WriteLine("Key: " + pair.Key.PadRight(20) + " Value: " + pair.Value);
  64. }
  65.  
  66. Console.Write ("\nNumber of items in the dictionary: " + dict.Count);
  67.  
  68.  
  69.  
  70. }
  71. }
  72. }
Success #stdin #stdout 0.06s 30828KB
stdin
Standard input is empty
stdout
Use the ContainsKey method to see if a movies exists in your dictionary:
Contains key Aprilia Rsv4       False
Contains key Yamaha R1M     False
Contains key Ducati V4s False

Contents of the dictionary:
Key: Aprilia Rsv4  Value: An Aprilia made sport bike
Key: Yamaha R1M  Value: Yamahas super sport bike
Key: ZX10RR    Value: Kawasakis Sport bike with a more performance built motor
Key: Ducati V4s  Value: Ducatis more track driven V4 model
Key: Honda CBR Fireblade  Value: Hondas new sport bike
Key: BMW S1000RR  Value: BMWs made sport bike
Key: GSXR 1000  Value: Suzukis liter sport bike

Just the keys:
Key: Aprilia Rsv4
Key: Yamaha R1M
Key: ZX10RR
Key: Ducati V4s
Key: Honda CBR Fireblade
Key: BMW S1000RR
Key: GSXR 1000

Just the values:
Value: An Aprilia made sport bike
Value: Yamahas super sport bike
Value: Kawasakis Sport bike with a more performance built motor
Value: Ducatis more track driven V4 model
Value: Hondas new sport bike
Value: BMWs made sport bike
Value: Suzukis liter sport bike

Contents of the dictionary sorted by keys:
Key: Aprilia Rsv4          Value: An Aprilia made sport bike
Key: BMW S1000RR           Value: BMWs made sport bike
Key: Ducati V4s            Value: Ducatis more track driven V4 model
Key: GSXR 1000             Value: Suzukis liter sport bike
Key: Honda CBR Fireblade   Value: Hondas new sport bike
Key: Yamaha R1M            Value: Yamahas super sport bike
Key: ZX10RR                Value: Kawasakis Sport bike with a more performance built motor

Number of items in the dictionary: 7