使用ContainsValue()
C#中的方法在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"); //搜索值 Console.WriteLine(d.ContainsValue("Two")); } }
输出结果
True