Dictionary建構函式的一些方法

Designed by Freepik
本篇紀錄Dictionary建構函式的一些方法
直接程式碼
圖片來源Designed by Freepik


  1. Dictionary.ContainsKey(TKey) 方法
(Dictionary內是否存在索引鍵)

// ContainsKey can be used to test keys before inserting them.

if (!openWith.ContainsKey("ht"))
{
  openWith.Add("ht", "hypertrm.exe");
  Console.WriteLine("Value added for key = \"ht\": {0}",
    openWith["ht"]);
}


// When a program often has to try keys that turn out not to
// be in the dictionary, TryGetValue can be a more efficient
// way to retrieve values.

string value = "";
if (openWith.TryGetValue("tif", out value))
{
  Console.WriteLine("For key = \"tif\", value = {0}.", value);
}
else
{
  Console.WriteLine("Key = \"tif\" is not found.");
}

沒有留言:

張貼留言

技術提供:Blogger.