C# 中的 ContainsValue


在 C# 中使用 ContainsValue() 方法搜尋 Dictionary 中的值。

建立一個 Dictionary 並新增元素 -

Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("keyboard", "One");
d.Add("mouse", "Two");

現在,使用 ContainsValue() 方法查詢特定值 -

d.ContainsValue("Two");

以下為完整程式碼 -

示例

 線上演示

using System;
using System.Collections.Generic;
using System.Linq;
class Program {
   static void Main() {
      Dictionary<string, string> d = new Dictionary<string, string>();
      d.Add("keyboard", "One");
      d.Add("mouse", "Two");
      // search for a value
      Console.WriteLine(d.ContainsValue("Two"));
   }
}

輸出

True

更新於: 2020 年 6 月 22 日

82 次瀏覽

開啟你的 事業

透過完成課程獲得認證

馬上開始
廣告
© . All rights reserved.