2014年1月31日 星期五

[C#] Ditionary

MSDN 範例

補充 : 初始化值

new Ditionary<TKey,TValue>()
{
 {"xxx","xxx"},{"yyy","yyy"},
{"zzz","zzz"},{"aaa","aaa"}
}



  1. using System;
  2. using System.Collections.Generic;
  3. public class Example
  4. {
  5.     public static void Main()
  6.     {
  7.         // Create a new dictionary of strings, with string keys.
  8.         //
  9.         Dictionary<stringstring> openWith =
  10.             new Dictionary<stringstring>();
  11.         // Add some elements to the dictionary. There are no
  12.         // duplicate keys, but some of the values are duplicates.
  13.         openWith.Add("txt""notepad.exe");
  14.         openWith.Add("bmp""paint.exe");
  15.         openWith.Add("dib""paint.exe");
  16.         openWith.Add("rtf""wordpad.exe");
  17.         // The Add method throws an exception if the new key is
  18.         // already in the dictionary.
  19.         try
  20.         {
  21.             openWith.Add("txt""winword.exe");
  22.         }
  23.         catch (ArgumentException)
  24.         {
  25.             Console.WriteLine("An element with Key = \"txt\" already exists.");
  26.         }
  27.         // The Item property is another name for the indexer, so you
  28.         // can omit its name when accessing elements.
  29.         Console.WriteLine("For key = \"rtf\", value = {0}.",
  30.             openWith["rtf"]);
  31.         // The indexer can be used to change the value associated
  32.         // with a key.
  33.         openWith["rtf"] = "winword.exe";
  34.         Console.WriteLine("For key = \"rtf\", value = {0}.",
  35.             openWith["rtf"]);
  36.         // If a key does not exist, setting the indexer for that key
  37.         // adds a new key/value pair.
  38.         openWith["doc"] = "winword.exe";
  39.         // The indexer throws an exception if the requested key is
  40.         // not in the dictionary.
  41.         try
  42.         {
  43.             Console.WriteLine("For key = \"tif\", value = {0}.",
  44.                 openWith["tif"]);
  45.         }
  46.         catch (KeyNotFoundException)
  47.         {
  48.             Console.WriteLine("Key = \"tif\" is not found.");
  49.         }
  50.         // When a program often has to try keys that turn out not to
  51.         // be in the dictionary, TryGetValue can be a more efficient
  52.         // way to retrieve values.
  53.         string value = "";
  54.         if (openWith.TryGetValue("tif"out value))
  55.         {
  56.             Console.WriteLine("For key = \"tif\", value = {0}."value);
  57.         }
  58.         else
  59.         {
  60.             Console.WriteLine("Key = \"tif\" is not found.");
  61.         }
  62.         // ContainsKey can be used to test keys before inserting
  63.         // them.
  64.         if (!openWith.ContainsKey("ht"))
  65.         {
  66.             openWith.Add("ht""hypertrm.exe");
  67.             Console.WriteLine("Value added for key = \"ht\": {0}",
  68.                 openWith["ht"]);
  69.         }
  70.         // When you use foreach to enumerate dictionary elements,
  71.         // the elements are retrieved as KeyValuePair objects.
  72.         Console.WriteLine();
  73.         foreach( KeyValuePair<stringstring> kvp in openWith )
  74.         {
  75.             Console.WriteLine("Key = {0}, Value = {1}",
  76.                 kvp.Key, kvp.Value);
  77.         }
  78.         // To get the values alone, use the Values property.
  79.         Dictionary<stringstring>.ValueCollection valueColl =
  80.             openWith.Values;
  81.         // The elements of the ValueCollection are strongly typed
  82.         // with the type that was specified for dictionary values.
  83.         Console.WriteLine();
  84.         foreach( string s in valueColl )
  85.         {
  86.             Console.WriteLine("Value = {0}", s);
  87.         }
  88.         // To get the keys alone, use the Keys property.
  89.         Dictionary<stringstring>.KeyCollection keyColl =
  90.             openWith.Keys;
  91.         // The elements of the KeyCollection are strongly typed
  92.         // with the type that was specified for dictionary keys.
  93.         Console.WriteLine();
  94.         foreach( string s in keyColl )
  95.         {
  96.             Console.WriteLine("Key = {0}", s);
  97.         }
  98.         // Use the Remove method to remove a key/value pair.
  99.         Console.WriteLine("\nRemove(\"doc\")");
  100.         openWith.Remove("doc");
  101.         if (!openWith.ContainsKey("doc"))
  102.         {
  103.             Console.WriteLine("Key \"doc\" is not found.");
  104.         }
  105.     }
  106. }
  107. /* This code example produces the following output:
  108. An element with Key = "txt" already exists.
  109. For key = "rtf", value = wordpad.exe.
  110. For key = "rtf", value = winword.exe.
  111. Key = "tif" is not found.
  112. Key = "tif" is not found.
  113. Value added for key = "ht": hypertrm.exe
  114. Key = txt, Value = notepad.exe
  115. Key = bmp, Value = paint.exe
  116. Key = dib, Value = paint.exe
  117. Key = rtf, Value = winword.exe
  118. Key = doc, Value = winword.exe
  119. Key = ht, Value = hypertrm.exe
  120. Value = notepad.exe
  121. Value = paint.exe
  122. Value = paint.exe
  123. Value = winword.exe
  124. Value = winword.exe
  125. Value = hypertrm.exe
  126. Key = txt
  127. Key = bmp
  128. Key = dib
  129. Key = rtf
  130. Key = doc
  131. Key = ht
  132. Remove("doc")
  133. Key "doc" is not found.
  134.  */

沒有留言:

張貼留言