本文讲述了c#hashtable怎么根据键查找值?具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:
设置元素的Hahtable集合。
Hashtable h = new Hashtable(); h.Add(1, "Jack"); h.Add(2, "Henry"); h.Add(3, "Ben"); h.Add(4, "Chris");
假设现在您需要找到一个值,然后使用该ContainsValue()方法。
我们在这里找到值“克里斯”-
h.ContainsValue(“Chris”);
using System; using System.Collections; public class Demo { public static void Main() { Hashtable h = new Hashtable(); h.Add(1, "Jack"); h.Add(2, "Henry"); h.Add(3, "Ben"); h.Add(4, "Chris"); Console.WriteLine("键和值列表:"); foreach (var key in h.Keys ) { Console.WriteLine("Key = {0}, Value = {1}",key , h[key]); } Console.WriteLine("Value Chris exists? "+h.ContainsValue("Chris")); Console.WriteLine("Value Tom exists? "+h.ContainsValue("Tom")); } }
输出结果
键和值列表: Key = 4, Value = Chris Key = 3, Value = Ben Key = 2, Value = Henry Key = 1, Value = Jack Value Chris exists? True Value Tom exists? False
更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。
长按或扫描下方二维码,免费获取 Python公开课和大佬打包整理的几百G的学习资料,内容包含但不限于Python电子书、教程、项目接单、源码等等
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!