page contents

c#hashtable怎么根据键查找值?

本文讲述了c#hashtable怎么根据键查找值?具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:

attachments-2022-11-SaveS4sG6363214daab3b.png

本文讲述了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电子书、教程、项目接单、源码等等

attachments-2022-10-kwwbZ9WG6347756cbf77c.jpg

  • 发表于 2022-11-03 10:03
  • 阅读 ( 845 )
  • 分类:C/C++开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
轩辕小不懂
轩辕小不懂

2403 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. Pack 1131 文章
  3. 小柒 1046 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章